Support the ongoing development of →
Requests Session
Last updated 2 months ago.

Ah, I saw this on Stack too.

Pasting for ease of access.

There appears to be a way to accomplish this using a session reject callback.

Relevant sources...

I can't find many references to this around the web, but reading more through the source it appears that if the session reject callback returns a truthy value, the session will be forced to use an array driver for the request rather than whatever is configured. Your callback also gets the current request injected so you can do some logic based on the request parameters.

I've only tested this on a local Laravel 4.2 install but it seems to work. You just need to bind a function to session.reject.

First, create a SessionRejectServiceProvider (or something like that)


use \Illuminate\Support\ServiceProvider;

class SessionRejectServiceProvider extends ServiceProvider {

    public function register()
        $me = $this;
        $this->app->bind('session.reject', function($app)use($me){
            return function($request)use($me){
                return call_user_func_array(array($me, 'reject'), array($request));
    // Put the guts of whatever you want to do in here, in this case I've
    // disabled sessions for every request that is an Ajax request, you
    // could do something else like check the path against a list and
    // selectively return true if there's a match.
    protected function reject($request)
        return $request->ajax();


Then add it to your providers in your app/config/app.php


return array(
   // ... other stuff
   'providers' => array(
       // ... existing stuff...

##Edit / More Info

The net result is that the reject() method is called on every request to your application, before the session is started. If your reject() method returns true, sessions will be set to the array driver and basically do nothing. You can find a lot of useful info the $request parameter to determine this, here's the API reference for the request object in 4.2.

Last updated 2 months ago.

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 - All rights reserved.