Problems with session Standard 5.3

Asked

Viewed 273 times

3

I’m having trouble setting and picking up items from a sessão, in this case I am setting which page should mark in the menu as active.

In the __contruct of mine controller I’m doing like this:

public function __construct()
{
    $this->middleware('auth');
    session()->put('pagina', 'I');
}

And on my pages where the menu is like this:

<li {{((Session('pagina') == 'H')? 'class="active"' : '')}}>
    <a href="/"><i class="zmdi zmdi-home"></i> Home</a>
</li>
<li {{((Session('pagina') == 'C')? 'class="active"' : '')}}>
    <a href="/igrejas"><i class="zmdi zmdi-assignment-account"></i> Igreja</a>
</li>
<li {{((Session('pagina') == 'I')? 'class="active"' : '')}}>
    <a href="/celulas"><i class="zmdi zmdi-accounts-alt"></i> {{ (($igreja->FLG_MODEL_IGREJ == 'C')? 'Celulas' : (($igreja->FLG_MODEL_IGREJ == 'P')? 'Pequenos Grupos' : 'Salas dominicais' )) }}</a>
</li>

But the menu is not coming marked, I gave a dd in Session and it’s coming back null, what to do ?

I tried to add also in functions of my controller and also had no success as in the example below:

public function index(){
    session()->put('pagina', 'I');


    if(Auth::user()->FLG_USUAR_MASTE == 1) {
        return view('aa');
    }else{
        return view('aa');
    }

}

1 answer

1


That’s specified and I don’t see why in this version (5.3) they did so, but the truth is that accessing the authenticated session/user in the constructor is no longer supported, not long ago either I’ve been "desperate" for a solution but I did not find a workaround that would work, my advice is to follow the documentation but in my case it didn’t work (later I noticed that it was because the version of my project was smaller than 5.3.4).

The advice I can give you in this case is to perform this operation session()->put('pagina', 'I'); in the method called by the route (or other) instead of in the constructor.

Here and in the documentation I see that:

Before using this Feature, make sure that your application is running Laravel 5.3.4 or above:

That is, you must be running a version 5.3.4 or higher, otherwise this solution will not work, and you should choose to use yours session in a method other than the constructor. To see the version your project is in: php artisan --version

Browser other questions tagged

You are not signed in. Login or sign up in order to post.