Add Routes and Use Session Information in Service Provider











up vote
3
down vote

favorite












tl;dr; How can I get session data, then based on authentication, manipulate routes in a laravel ServiceProvider?



The Problem



I need to do two things in a service provider. Register routes and use session data at the same time potentially.



Existing Solutions



Route Without Session Data



I know ServiceProvider::register may happen before RouteProvider::register gets called. ServiceProvider::boot can add routes using something like this...



public function boot(Router $router)
{
$router->prefix('api')->group(function ($router) {
$router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
});
}


Now this will add the route properly. But by the time that route is accessed, there will be no session data. Therefore Auth::user() will return null. And of course Auth::user() is going to return null in the boot() method as well during the adding of routes.



Session Data Without Route



I could extend the IlluminateSessionMiddlewareStartSession with my own middleware and fire an event like session.started at the end of an overloaded startSession() method. With this, I should have the session data needed to do something like Auth::user().



public function boot(Router $router)
{
Event::listen('session.started', function () use ($router) {
if (Auth::user()->can('do.something')) {
$router->middleware(['auth'])->prefix('api')->group(function ($router) {
$router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
});
}
});
}


However, the route will not be added at this point as that stage has already been long over with.



Proper Solution Hints



I've been leaning towards registering my routes, then in my controller, I would somehow inject the session middleware so that it starts the session before my controller code actually runs. But I'm unsure how to do something like that.



It would be very nice if I could have access to the session data before even supplying my routes as it would be cleaner, faster and more secure to include routes that users have access to in the first place instead of checking on every call to them or removing them after they've been added and authorization has been checked.



References




  • How to retrieve session data in service providers in laravel?

  • https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process

  • https://github.com/laravel/framework/issues/7906

  • https://github.com/laravel/framework/pull/7933

  • https://laravel.io/forum/12-17-2014-session-content-not-available-in-service-providers-register

  • https://josephsilber.com/posts/2017/01/23/getting-current-user-in-laravel-controller-constructor










share|improve this question


























    up vote
    3
    down vote

    favorite












    tl;dr; How can I get session data, then based on authentication, manipulate routes in a laravel ServiceProvider?



    The Problem



    I need to do two things in a service provider. Register routes and use session data at the same time potentially.



    Existing Solutions



    Route Without Session Data



    I know ServiceProvider::register may happen before RouteProvider::register gets called. ServiceProvider::boot can add routes using something like this...



    public function boot(Router $router)
    {
    $router->prefix('api')->group(function ($router) {
    $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
    });
    }


    Now this will add the route properly. But by the time that route is accessed, there will be no session data. Therefore Auth::user() will return null. And of course Auth::user() is going to return null in the boot() method as well during the adding of routes.



    Session Data Without Route



    I could extend the IlluminateSessionMiddlewareStartSession with my own middleware and fire an event like session.started at the end of an overloaded startSession() method. With this, I should have the session data needed to do something like Auth::user().



    public function boot(Router $router)
    {
    Event::listen('session.started', function () use ($router) {
    if (Auth::user()->can('do.something')) {
    $router->middleware(['auth'])->prefix('api')->group(function ($router) {
    $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
    });
    }
    });
    }


    However, the route will not be added at this point as that stage has already been long over with.



    Proper Solution Hints



    I've been leaning towards registering my routes, then in my controller, I would somehow inject the session middleware so that it starts the session before my controller code actually runs. But I'm unsure how to do something like that.



    It would be very nice if I could have access to the session data before even supplying my routes as it would be cleaner, faster and more secure to include routes that users have access to in the first place instead of checking on every call to them or removing them after they've been added and authorization has been checked.



    References




    • How to retrieve session data in service providers in laravel?

    • https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process

    • https://github.com/laravel/framework/issues/7906

    • https://github.com/laravel/framework/pull/7933

    • https://laravel.io/forum/12-17-2014-session-content-not-available-in-service-providers-register

    • https://josephsilber.com/posts/2017/01/23/getting-current-user-in-laravel-controller-constructor










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      tl;dr; How can I get session data, then based on authentication, manipulate routes in a laravel ServiceProvider?



      The Problem



      I need to do two things in a service provider. Register routes and use session data at the same time potentially.



      Existing Solutions



      Route Without Session Data



      I know ServiceProvider::register may happen before RouteProvider::register gets called. ServiceProvider::boot can add routes using something like this...



      public function boot(Router $router)
      {
      $router->prefix('api')->group(function ($router) {
      $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
      });
      }


      Now this will add the route properly. But by the time that route is accessed, there will be no session data. Therefore Auth::user() will return null. And of course Auth::user() is going to return null in the boot() method as well during the adding of routes.



      Session Data Without Route



      I could extend the IlluminateSessionMiddlewareStartSession with my own middleware and fire an event like session.started at the end of an overloaded startSession() method. With this, I should have the session data needed to do something like Auth::user().



      public function boot(Router $router)
      {
      Event::listen('session.started', function () use ($router) {
      if (Auth::user()->can('do.something')) {
      $router->middleware(['auth'])->prefix('api')->group(function ($router) {
      $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
      });
      }
      });
      }


      However, the route will not be added at this point as that stage has already been long over with.



      Proper Solution Hints



      I've been leaning towards registering my routes, then in my controller, I would somehow inject the session middleware so that it starts the session before my controller code actually runs. But I'm unsure how to do something like that.



      It would be very nice if I could have access to the session data before even supplying my routes as it would be cleaner, faster and more secure to include routes that users have access to in the first place instead of checking on every call to them or removing them after they've been added and authorization has been checked.



      References




      • How to retrieve session data in service providers in laravel?

      • https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process

      • https://github.com/laravel/framework/issues/7906

      • https://github.com/laravel/framework/pull/7933

      • https://laravel.io/forum/12-17-2014-session-content-not-available-in-service-providers-register

      • https://josephsilber.com/posts/2017/01/23/getting-current-user-in-laravel-controller-constructor










      share|improve this question













      tl;dr; How can I get session data, then based on authentication, manipulate routes in a laravel ServiceProvider?



      The Problem



      I need to do two things in a service provider. Register routes and use session data at the same time potentially.



      Existing Solutions



      Route Without Session Data



      I know ServiceProvider::register may happen before RouteProvider::register gets called. ServiceProvider::boot can add routes using something like this...



      public function boot(Router $router)
      {
      $router->prefix('api')->group(function ($router) {
      $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
      });
      }


      Now this will add the route properly. But by the time that route is accessed, there will be no session data. Therefore Auth::user() will return null. And of course Auth::user() is going to return null in the boot() method as well during the adding of routes.



      Session Data Without Route



      I could extend the IlluminateSessionMiddlewareStartSession with my own middleware and fire an event like session.started at the end of an overloaded startSession() method. With this, I should have the session data needed to do something like Auth::user().



      public function boot(Router $router)
      {
      Event::listen('session.started', function () use ($router) {
      if (Auth::user()->can('do.something')) {
      $router->middleware(['auth'])->prefix('api')->group(function ($router) {
      $router->resource('lists', 'AppExtensionsExtensionNameHttpControllersMyController');
      });
      }
      });
      }


      However, the route will not be added at this point as that stage has already been long over with.



      Proper Solution Hints



      I've been leaning towards registering my routes, then in my controller, I would somehow inject the session middleware so that it starts the session before my controller code actually runs. But I'm unsure how to do something like that.



      It would be very nice if I could have access to the session data before even supplying my routes as it would be cleaner, faster and more secure to include routes that users have access to in the first place instead of checking on every call to them or removing them after they've been added and authorization has been checked.



      References




      • How to retrieve session data in service providers in laravel?

      • https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-process

      • https://github.com/laravel/framework/issues/7906

      • https://github.com/laravel/framework/pull/7933

      • https://laravel.io/forum/12-17-2014-session-content-not-available-in-service-providers-register

      • https://josephsilber.com/posts/2017/01/23/getting-current-user-in-laravel-controller-constructor







      php laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 at 20:23









      Xedecimal

      2,60011421




      2,60011421





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344864%2fadd-routes-and-use-session-information-in-service-provider%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344864%2fadd-routes-and-use-session-information-in-service-provider%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]