Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrdezzods's avatar

Laravel 5.2 binding session object to router

Im having a trouble binding a object in session to the router. This is in a fresh install of Laravel 5.2 and this is what i have:

So my routes file:

Route::resource('carts', 'CartsController');

In RouteServiceProvider:

public function map(Router $router)

{

$router->group(['namespace' => $this->namespace], function ($router) {
    $router->bind('carts', function ($value) {
        return Cart::get($value);
    });
    require app_path('Http/routes.php');
});

} and this is in the Cart class:

public static function get($cartId)

{ if (\Session::has('carts.' . $cartId)) { $cart = \Session::get('carts.' . $cartId); } else { $cart = new Cart($cartId); } return $cart; } So untill this step Session is not loaded. I tried to starting session manually by calling \Session::start(); but then it wouldn't get the expected data even if there are cart object in session. I also tried dd(\Session::all()), but it returns an empty array.

Does anyone know when to bind a object to route so that the session is also loaded?

0 likes
11 replies
jhoff's avatar

I don't think you're supposed to do route model binding in RouteServiceProvider@map. According to the documentation, you're supposed to do it in RouteServiceProvider@boot:

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        parent::boot($router);
        
        $router->bind('carts', function ($value) {
            return Cart::get($value);
        });
    }
mrdezzods's avatar

@jhoff , Yep i had tried that aswell. Session is still not loaded. In fact its not loaded until i run the Cart::get($value) from my controller. Do you have any other ideas,maybe?

jhoff's avatar

Are your routes using the new web middleware?

Route::group(['middleware' => 'web'], function() {
    Route::resource('carts', 'CartsController');
});
1 like
mrdezzods's avatar

@jhoff , can you check it for yourself if the Session is loaded in router or not?

jhoff's avatar

The default web middleware should be starting the Session:

app\Http\Kernel.php

    protected $middlewareGroups = [
        'web' => [
            ...
            \Illuminate\Session\Middleware\StartSession::class,

Maybe it's an issue with using the facade? Does it work to use app()->session() instead?

1 like
d3xt3r's avatar

AFAIK, your middlewares wont be called unless the service providers are registered and booted. So no session iinfo will be available for you to use.

1 like
thomaskim's avatar

Well, first, there are a few things that I would change or a few things that I just don't understand.

  1. You named your function get. It's kind of confusing because it behaves vastly different from Laravel's own get method.

  2. I'm assuming $cartId is an integer. Laravel model's constructor function accepts an array, not an integer.

Finally, regarding your problem, you cannot use sessions inside the RouteServiceProvider because Laravel sessions haven't even been initialized yet. There's a huge discussion on that here:

https://github.com/laravel/framework/issues/6118

Basically, people are saying that it doesn't make sense to run route model binding before middlewares (which is what initiates the sessions). Some are arguing that it does make sense.

In L5.2, this lifecycle kind of caused other issues with the new implicit model binding.

Anyway, a workaround is to move the web middleware into the global middleware stack. This means getting rid of your web middleware. So open up app\Http\Kernel.php and change these two arrays:

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \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,
    ];
    protected $middlewareGroups = [
        'api' => [
            'throttle:60,1',
        ],
    ];

Also, make sure to remove the web group in your routes file.

1 like
mrdezzods's avatar

thanks for the info @thomaskim , I tried adding StartSession middleware to global middleware stack. That didn't help either. I'm gonna try putting all the middlewares from web to global. Hope that will help. and for the record, Cart is not a Modal class, its my own class in different namespace .

thomaskim's avatar

Oh okay. Sorry, didn't know that about your Cart class.

Regarding the middlewares, yes, my example moved the entire web group to the global middleware stack. :)

1 like

Please or to participate in this conversation.