[5.0] Undefined index: _sf2_meta when changing Session/Cookie driver in middleware
I want to change session and cookie driver to arrays for some routes (API request), so in 4.2 I could do it in the filter and it worked fine:
\Config::set('session.driver', 'array');
But in L5, something has changed and I cannot achieve the same effect in Middleware.
How to reproduce: Create a middleware class and add it to application Kernel middleware array.
class TestSessionDriver implements Middleware{
public function handle($request, Closure $next){
\Config::set('session.driver', 'array');
return $next($request);
}
}
Result:
ErrorException in compiled.php line 10924:
Undefined index: _sf2_meta
It seems that I'm not the only one with the problem, I can find many posts about this on google, but without any solutions.
Try php artisan clear-compiled to get a proper error stack logged so that we can better verify the issue.
Also, make sure that the you add the TestSessionDriver before the StartSession middleware.
I don't think this is a bug, and you could get better help in the forums or IRC.
This is the full stack trace:
[2015-03-02 11:25:40] local.ERROR: exception 'ErrorException' with message 'Undefined index: _sf2_meta' in \vendor\laravel\framework\src\Illuminate\Session\Store.php:280
Stack trace:
#0 \vendor\laravel\framework\src\Illuminate\Session\Store.php(280): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', 'C:\\project...', 280, Array)
#1 \vendor\laravel\framework\src\Illuminate\Session\Store.php(251): Illuminate\Session\Store->addBagDataToSession()
#2 \vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(89): Illuminate\Session\Store->save()
#3 \vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(129): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\JsonResponse))
#4 \public\index.php(57): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\JsonResponse))
#5 {main}
This is my middleware class:
class ApiRequest implements Middleware{
public function handle($request, Closure $next){
\Config::set('session.driver', 'array');
return $next($request);
}
}
I can easily replicate it on a clean laravel 5.0.1 install.
Just modify the existing VerifyCsrfToken middleware, add one line:
public function handle($request, Closure $next)
{
\Config::set('session.driver', 'array');
return parent::handle($request, $next);
}
From @ibrasho comment:
make sure that the you add the ApiRequest before the StartSession middleware.
ApiRequest It is a route middleware. The VerifyToken middleware is defined after Session middleware.
Ok, but how can I add this functionality to a route middleware?
Session::setDefaultDriver(NULL);
@malcolmhall The problem was solved by moving middleware above StartSession middleware.
@briedis I am having this same issue. How did you move the ApiRequest middleware before StartSession if it is still a route middleware? Or did you change to a regular middleware and do a check for urls inside the middleware? Thanks
@keyurshah It was a regular middleware.

StartSession has to be below.
And in the middleware:
public function handle($request, Closure $next)
{
$path = $request->getPathInfo();
if (strpos($path, '/api/v') === 0) {
\Config::set('session.driver', 'array');
\Config::set('cookie.driver', 'array');
}
return $next($request);
}
Ugly, but I didn't know how to solve it better, using route middlewares.
thanks for the code. yeah, wish there was a way to do it directly in route middleware like how you and I used to do it in laravel 4.
this is turning out to be a real problem, as I have a lot of urls that can't be distinguished from each other easily. This is to allow nginx to proxy cache certain pages by disabling the session/cookie. I was doing it just the way as described in laravel 4 with filters.
I really need it to be route middleware based. Can anyone think of any other workarounds? thanks.
I was able to remove the error above by setting the session driver to null instead of array:
Config::set('session.driver', null);