mardi 16 octobre 2018

Using Auth::check() in class?

I know that since Laravel 5.3

you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

as stated here. There is also explained how to create a workaround for controller. However, I want to call Auth in a constructor of a model which is bootstrapped.

To be more precise, I am using the Laravel-localization package. The function setLocale from the LaravelLocalization class is called in route/web.php

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
],

Inside the function setLocale() the call Auth::check() always returns false, even if a user is logged in:

public function setLocale($locale = null)
{
   // \Auth::check is always false..
   if(\Auth::check()){

How can I use Auth::check() here?


For those who wonder why I need auth:check - not every language my-domain.com/de/article-1 is public. Some of them should only be visible to editors, but not to any user.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire