Laravel Routing Cache in Custom Route Files

I am building a project in Laravel 5.2 that has a large one (large, as in many lines) routes.php. To make the routes a little clean for the eyes, I divided all the route groups separated by the attached files. In app\Http\Routes\.

I need all the files in RouteServiceProviderwhich works (hotfix: worked ..) is fine for me. After all this, I wanted to cache the routes using php artisan route:cache. Then, if you go to the page, all you have is a 404 error.

The β€œlong” story is short: the new routing logic crashes AFTER AFTER the route category.

This is my display function in RouteServiceProvider(inspired by this answer):

public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        // Dynamically include all files in the routes directory
        foreach (new \DirectoryIterator(app_path('Http/Routes')) as $file)
        {
            if (!$file->isDot() && !$file->isDir() && $file->getFilename() != '.gitignore')
            {
                require_once app_path('Http/Routes').DS.$file->getFilename();
            }
        }
    });
}

- , ? route.php, . .

+4
1

TL; DR: require require_once, .

, , Illuminate\Foundation\Console\RouteCacheCommand.
, getFreshApplicationRoutes, .

, :

$app = require $this->laravel->bootstrapPath().'/app.php';

$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

$routesCnt = $app['router']->getRoutes()->count();

$this->info($routesCnt);

, .

, , Http\Routes, . "require" "require_once". !

.

, , - ( ). .json:

    "psr-4": {
        "App\\": "app/"
    }

, / . , , , . , , *_once.

RouteServiceProvider.php, map, :

$router->group(['namespace' => $this->namespace], function ($router) {
        // Dynamically include all files in the routes directory
        foreach (new \DirectoryIterator(app_path('Http/Routes')) as $file)
        {
            $path = app_path('Http/Routes').DIRECTORY_SEPARATOR.$file->getFilename();
            if ($file->isDot() || $file->isDir() || $file->getFilename() == '.gitignore')
                continue;

            require $path;
            $included[] = $path;
        }
    });
+4

All Articles