V
V
Vanik Khachatryan2018-02-19 21:35:59
Laravel
Vanik Khachatryan, 2018-02-19 21:35:59

Why don't sessions work in Services?

public function boot()
    {
        // Задаеться роль гостя, если он не авторизован
        if(!Session::has('id_user')){
            Session::put('role', 'guest');
        }

        // Глобальная переменная о пользователе
        $myInfo = Users::find(Session::get('id_user'));
        view()->share('userInfo', $myInfo);

    }

If you enter the user id itself into find, then everything works fine, but it refuses with sessions, why, how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Nesmeyanov, 2018-02-20
@SerafimArts

The session is started from the middleware during the request/response. Starting, if I'm not mistaken, with Laravel 5.2.
Pyshch: https://github.com/laravel/laravel/blob/master/app...
The boot method starts earlier, which means it has no access to sessions. And this is quite logical, because, for example, there cannot be sessions in the CLI, which means that it makes no sense to launch and use them in the provider. It makes sense to register the view composer, because it resolves directly during the display of the view, which means that sessions may well already be there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question