1

i am trying to store some data on load base controller of Laravel and I used constructor to do this and constructor executes successfully. But when i am storing value in session and then refresh the page next time that same session value showing null:

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Language;
use Session;
class Controller extends BaseController
{
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    function __construct()
    {
        if(Session::get('phrase') == null)
        {
            Session::put('phrase','test');
            dump(Session::get('phrase'));
        }
    }
}

Accessing through:

class DashboardController extends Controller
{
  public function index()
  {
     if(!in_array(Auth::user()->role_id,[1,2])){
         return redirect()->route('orsers.list',['filters'=>'']);
     }
     return view('dashboard.index');
  }

i can't understand where i am wrong??

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Rahul Sharma
  • 406
  • 3
  • 5
  • 17
  • Are you sure you are not clearing the value somewhere? Like `Session::pull` anywhere in the code? Are you switching midlewares in the `refresh` process (or right after you set/read the session) ? Can you actually store the session values on the drive? Maybe you have read only permissions for session storage folder? – Peon Apr 05 '17 at 05:41
  • no.. i am running dashboard controller index function and here its code...(check updated question) – Rahul Sharma Apr 05 '17 at 05:43
  • same behave in `DashboardController` constructor – Rahul Sharma Apr 05 '17 at 05:47
  • Maybe [this](http://stackoverflow.com/questions/23917409/laravel-session-data-not-sticking-across-page-loads) can help, otherwise, it's too broad to test are comment further. – Peon Apr 05 '17 at 05:52
  • sir i got the answer `we can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.` – Rahul Sharma Apr 05 '17 at 05:56
  • http://stackoverflow.com/questions/39186222/laravel-5-3-auth-check-in-constructor-returning-false/39188299#39188299 – Rahul Sharma Apr 05 '17 at 05:56

1 Answers1

1

In your app/Http/Kernel.php rearrange the following middlwaregroups...

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

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

Let me know if your $middlewareGroups is already ordered like this and we can debug further.

Rahi
  • 1,435
  • 1
  • 12
  • 20