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.
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!
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.
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.
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.
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?
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
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 likeSession
orCache
or whatever, you should place your code withinboot()
method which is fired when all service providers registered it's stuff. Thenboot()
method is called in every provider, if it exists, in order in which they are placed in configuration array.
namespace App\Providers;
use Session;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider {
public function register() {
}
public function boot(){
print_r(Session::all());
exit();
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community