Asked  2 Years ago    Answers:  5   Viewed   234 times

In new laravel I can't get session in constructor. Why?

public function __construct()
{
    dd(Session::all()); //this is empty array
}

and then below

public function index()
{
    dd(Session::all()); //here works
}

In old laravel i remember there was not this problem. something changed?

 Answers

5

You can't do it by default with Laravel 5.3. But when you edit you Kernel.php and change protected $middleware = []; to the following it wil work.

protected $middleware = [
    IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
    IlluminateSessionMiddlewareStartSession::class,
    IlluminateViewMiddlewareShareErrorsFromSession::class,
];

protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,

        AppHttpMiddlewareVerifyCsrfToken::class,
        IlluminateRoutingMiddlewareSubstituteBindings::class,
    ],
    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

Hope this works!

Wednesday, December 21, 2022
 
2

It's worth noting that Laravel actually only stores active sessions in the session folder; those that are 'remembered' but haven't been actively accessing your application do not count among these sessions. As such, just deleting the files won't finish the job.

You must also clear all Remember Tokens from the users table, as this token is used in conjunction with the local token stored in the cookie to re-create the session when a user reconnects.

Monday, September 26, 2022
 
2

In order to get remember me functionality you shouldn't work with session configuration. Instead, you should use the functionality already built into Laravel.

When you authenticate user with Auth::attempt(), you can pass a boolean value as the second argument. Set it to true, if user checked remember me checkbox. Set it to false otherwise.

This way, when checkbox is checked, user will be remembered till they manually log out of application. Otherwise, they'll be remembered till they close the browser or session expires.

You can find some more informaton here: https://laravel.com/docs/5.3/authentication#remembering-users

Thursday, October 13, 2022
 
philip
 
5

Your $rr variable contains an object of class HootlexFriendshipsModelsFriendship - you can see its source code here: https://github.com/hootlex/laravel-friendships/blob/master/src/Models/Friendship.php.

As you can see, this model allows you to access both sender and recipient of given friend request via properties of the same name.

Therefore, in order to get username of the sender, you'll need to access it with:

$rr->sender->username
Saturday, August 27, 2022
 
5

As you've added a new method into a resourceful controller, you should register the new method first, before the resource.

E.g.

<?php // Routes.php

Route::get('/admin/login', '[email protected]');
Route::resource('admin', 'AdminController');

This way your before filters should work as you have then like this:

<?php // AdminController.php
   class AdminController extends BaseController {
     function __construct() {
       $this->beforeFilter('admin', array('except' => array('adminLogin')));
      $this->beforeFilter('csrf', array('on' => 'post'));
    }
}
Sunday, December 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages