how to use both cookies and files session driver in laravel
i have admin and pos page in where all authentication data store in cookies. but now in pos page I, store all products data list in session('products') which is more than 4kb. now i want store the products session in file session driver. when i use the below code in AppServiceProvider; the session driver has set to file and i can't access the page becasue of auth middleware
if (request()->is('admin/*')) {
\Config::set('session.driver','file');
\Config::set('session.cookie', 'file_session');
}
@kundefine To use both cookies and files session driver in Laravel, you can create a custom middleware that checks the current URL and sets the session driver accordingly.
Create a new middleware: php artisan make:middleware SessionDriverMiddleware
In the handle method of the middleware, you can check the current URL and set the session driver accordingly:
public function handle($request, Closure $next)
{
if ($request->is('pos/*')) {
config(['session.driver' => 'file']);
} else {
config(['session.driver' => 'cookie']);
}
return $next($request);
}
Register the middleware in the app/Http/Kernel.php file under the web middleware group
You can also set a different cookie name for the file session driver:
config(['session.files' => 'file_session']);
By this way, all requests to the pos routes will use the file session driver and all other routes will use the cookie session driver.
@tisuchi what u say i exactly doing this.. but now i face two problems. csrf token miss match problem and another is, data is not storing to the sessions. temporarily i solved csrf token solution vai exclude the url. but the session is not storing now.😢
Why store it in session? Why not use the database? Or even cache? Or cookies. Session is for storing small amounts of data like user id, flash messages and similar