DEV Community


Laravel 7: Cache route error

David Valbuena
・1 min read

It is common to create laravel projects in subfolders, and perform an access configuration by subdomains, something similar to: , 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 = [
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)

Discussion (0)