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

alekslyse's avatar

Access user session in routes

I'm trying to make a context aware laravel installation where I want to use a session to override the multi-url platform. With that I'm in need to access the request->session storage in routes.php, but from what I know sessions are not available in routes.php.

If its somehow to start the session in routes.php what would be needed to start it (not via php start session, only looking for "laravel" methods)

Also I was thinking if possible to wrap the routes with a group container and run a middleware with the work I wanted to have done, so with that is is possible to do the following flow

  • Create a group on /
  • Add a middleware on that group with an array input
  • Run the middleware that is session aware
  • Process the array IN the middleware and pass on
  • When now the middlewares are done and INSIDE the group, I can access the modified array.

Is that possible, and if so how do I access or pass on a variable I made in a middleware?

0 likes
2 replies
otepas's avatar

I use middleware for a similar situation: multi-tenant app that does not show the tenant_id in the url.

Answering your questions:

You don't need to create a group on /. If you need to run that middleware for all your routes, just register is as a global middleware. http://laravel.com/docs/5.1/middleware#registering-middleware

I don't know exactly what you mean by processing the array in the middleware. In the middleware you receive the $request as a parameter, and you send that $request to the next middleware or the controller when you call return $next($request);

I haven't tested modifying the $request in the middleware, but I think you should be able to do something like this:

public function handle($request, Closure $next)
    {
        // Do processing
    // Modify $request

        return $next($request);
    }

That way you can add the information you want to your $request and have it available in your controller.

alekslyse's avatar

With processing in the middleware I mean the following. To avoid running each module on say /module1 /module2 and need an awareness of "subdomains" I wanted to include module specific routes with its own namespace. So based on context based on a routing table someurl routes to module 1 on / and anotherurl routes to module 2 on /.

What I wanted is to take the routing table array and check if the user has changed the module context, so the same url should route to another module. To make that work I have to create a session to say something like module_context = routeID that will override the database for that session only.

The problem with the request is you can access the session while being in middleware, but when accessing the request in the group (where I include the context routes) the request loose the sessions (all sessions including the laravel ones). I passed the routing array via a use statement on the function, but that doesnt solve my problem I need to check for the context

Here is an example of how I route is with a cookie, but I wanted to use a session instead

//Checking if we have set a context override
if (Request::hasCookie('module_context'))
{
    $context = Crypt::decrypt(Request::cookie()['module_context']);

}
else
    $context = null;

//dd(Crypt::decrypt(Request::cookie()['test']));

$path = app_path('Http/Routes');

use App\Models\DomainRoute;

//Default Routes Should Always Run
$loadRoutes = array('Shared',
                    'Default',
                    'Administration',
                    'User');


$router = new DomainRoute();
$newRoute = $router->getRoute(Request::getHost(), $context);




//Add the route if it exist
if ($Addroute = addRoute($newRoute, $loadRoutes))
    $loadRoutes[] = $Addroute;



function addRoute($route, $routes)
{
    if (!in_array($route, $routes))
        return $route;
    else return false;
}







//Standard group at lowest level to take care of localization

Route::group(['middleware' => ['cors'], 'prefix' => LaravelLocalization::setLocale()],
    function() use ($loadRoutes)
    {


        // Dynamically include the needed routes
        foreach ($loadRoutes as $file)
        {
            if (file_exists(__DIR__.'/routes/'.$file.'.php'))
            {
                require_once (__DIR__.'/routes/'.$file.'.php');
            }
        }

    });

Please or to participate in this conversation.