Support the ongoing development of Laravel.io →
posted 9 years ago
Session
Last updated 2 years ago.
0

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.

0

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!

0

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.

0

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.

0

Thanks for your help and advice!

0

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.

0

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?

0

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

Last updated 9 years ago.
0

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.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

KuenzelIT kuenzelit Joined 9 Jul 2014

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.