DEV Community

David Valbuena
David Valbuena

Posted on

Laravel 7: Cache route error

It is common to create laravel projects in subfolders, and perform an access configuration by subdomains, something similar to: https://mydomain.com/aliasdomain , generally, this has been supported and at the time of caching the routes hasn't been generated a type of conflict, however, in laravel 7 this has generated a mistake in the routes that are specified as:

Route::get('/', '.....')
Enter fullscreen mode Exit fullscreen mode

A solution implemented in a project to solve this problem with the route cache was:

  1. Create a middleware with the following validation:
class RouteCache 
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->path() === '/') {
            $newURI = $request->server->get('REQUEST_URI') . 'index.php';
            $request->server->set('REQUEST_URI', $newURI);
        }

        return $next($request);
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Add the middleware to the Kernel file in the middleware array:
protected $middleware = [
    ....,
    \App\Http\Middleware\RouteCache::class,
];
Enter fullscreen mode Exit fullscreen mode

With this, what is achieved is that if a route / is declared within the project, the middleware validates when trying to access it and overwrites the REQUEST_URI value that the request carries by adding index.php (this will continue being transparent to the end user)

Top comments (0)