Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel application change session path dynamically

The laravel application url will be something like app.laravel.com\{clientName}. All the routes will be following the client_name, for example app.laravel.com\{clientName}\home, app.laravel.com\{clientName}\profile. Will load/ render the application depends on the clientName.

routes/web.php

Route::group(['prefix' => '{clientName}', 'middleware' => 'appclient'], function () {
    Route::get('/', 'ClientController@index');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout');
    Route::get('home', 'HomeController@index');
});

In the appclient middleware

public function handle($request, Closure $next) {
    $clientName = explode('/', $request->path())[0];
    $client = Client::where('clientName', $clientName)->first();

    if(!isset($client->id)) {
        abort(404);
    }

    Config::set('session.path', "/$clientName");

    return $next($request);
}

What I'm trying to achieve is set the session based on the clientName directory. When I login I'm getting TokenMismatchException.

First question

Can I store the session based on url with directory like app.laravel.com\{clientName} ?

Second Question

I saw there is a setting session.path, what above I tried is to use that approach. If that is possible, how can I fixed this issue? Is it a good idea to updating the session path in the middleware?

Appreciate any feed back or other approaches

UPDATE

  • Using Redis as session driver
  • In my further investigation the request session token every time generates new one
like image 913
Saumini Navaratnam Avatar asked Oct 07 '16 06:10

Saumini Navaratnam


Video Answer


1 Answers

What I did is updated the session.path & session.cookie dynamically.

Config::set('session.path', "$clientName");
Config::set('session.cookie', $clientName.'_laravel_session');

This is currently working for me.

like image 95
Saumini Navaratnam Avatar answered Nov 08 '22 07:11

Saumini Navaratnam