Add Routes and Use Session Information in Service Provider












3














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



























    3














    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

























      3












      3








      3







      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 '18 at 20:23









      Xedecimal

      2,61011421




      2,61011421
























          0






          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',
          autoActivateHeartbeat: false,
          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

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents