Back

Session content not available in Service Providers register()


KuenzelIT posted 4 years ago

Hey,

I'm currently having a big problem with accessing the Session content inside of a Service Providers register-method. The content just isn't there at this point and gets loaded somewhere after my Service Provider. I couldn't find when and where it does get loaded.

Can someone explain to me when and why "so late" the content is being loaded?

The Session has some groups listed where the current user is a member and I want to check if the user is a member of a specific group.

Thanks!

zaalbarxx replied 4 years ago

You don't have the access to the session because the service provider that load session is not available at this time. register() method in service provider should only be used for binding specific things and do a couple more things. But if you want to be sure that all service providers already registered their bindings and make use of anything like Session or Cache or whatever, you should place your code within boot() method which is fired when all service providers registered it's stuff. Then boot() method is called in every provider, if it exists, in order in which they are placed in configuration array.

KuenzelIT replied 4 years ago

Hmm, alright. I get that somehow. I assume the Session's boot method loads the content then, so it's only available later?

I'm going to use the boot method to do this check then.

My aim was to stop before the service providers routes are defined, so that the user can't access them, if he is'nt in a specific group. Is it ok to define the routes after my check in the boot-method then?

Thank you for your help!

zaalbarxx replied 4 years ago

Why can't you just register everything including your routes then apply a filter to all your restricted routes and check if user is eligible to access them ? :) This is a more natural way to achieve it, I think.

KuenzelIT replied 4 years ago

Yeah, if it were only routes. But a package is also registering navigation elements and may do some other stuff, the current user may should not see/have. That's the reason I thought it would be best to stop the package as soon as possible, also to save some performance.

KuenzelIT replied 4 years ago

Thanks for your help and advice!

zhuston replied 4 years ago

I found that in Laravel 5 it seems there is a new StartSession Middleware that runs after all of the service providers have been booted. I am not certain if there wasn't another cause but it seemed I couldn't access the session from boot() in a serviceprovider in Laravel 5.

mtvs replied 4 years ago

zhuston said:

I found that in Laravel 5 it seems there is a new StartSession Middleware that runs after all of the service providers have been booted. I am not certain if there wasn't another cause but it seemed I couldn't access the session from boot() in a serviceprovider in Laravel 5.

me, too. How can I use the session data when the app is booting?

mtvs replied 4 years ago

It seems you should write a middleware to access the session data in the boot process..

Laravel 5 has such a middleware out of the box to share $errors in the views from the session. Illuminate\View\Middleware\ShareErrorsFromSession

vinoddice replied 2 years ago

zaalbarxx said:

You don't have the access to the session because the service provider that load session is not available at this time. register() method in service provider should only be used for binding specific things and do a couple more things. But if you want to be sure that all service providers already registered their bindings and make use of anything like Session or Cache or whatever, you should place your code within boot() method which is fired when all service providers registered it's stuff. Then boot() method is called in every provider, if it exists, in order in which they are placed in configuration array.

i have coding

namespace App\Providers;

use Session;
use Illuminate\Support\ServiceProvider;

class ConfigServiceProvider extends ServiceProvider {
    public function register() {

    }
    public function boot(){
        print_r(Session::all());
        exit();
    }
}

this is not working session array is empty find.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us