Laravel 自定义路由文件上的路由缓存
Laravel route cache on custom route files
我正在 Laravel 5.2 中构建一个项目,它有一个很大的(在很多行中很大)routes.php
。为了让路线看起来更清晰一些,我将所有路线组拆分成单独的文件。在 app\Http\Routes\
.
我需要 RouteServiceProvider
中的所有文件(更正:有效..)对我来说非常好。毕竟我想用 php artisan route:cache
缓存路由。然后,如果你转到一个页面,你得到的只是一个 404 错误。
“长话短说”:新路由逻辑在 artisan 路由缓存后崩溃。
这是我在 RouteServiceProvider
中的地图函数(灵感来自 this 答案):
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();
}
}
});
}
有人知道问题出在哪里吗?或者如果我想使用路由缓存,我是否只需要将所有内容放回 routes.php 中。提前致谢。
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
文件夹中添加了多少文件,其中的 none 已注册。
所以我决定尝试使用 "require" 而不是 "require_once"。
瞧!
路线数量适当增加。
至于为什么会这样,我猜是因为作曲家自动加载(这只是一个有根据的猜测)。
看看 composer.json:
"psr-4": {
"App\": "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;
}
});
我正在 Laravel 5.2 中构建一个项目,它有一个很大的(在很多行中很大)routes.php
。为了让路线看起来更清晰一些,我将所有路线组拆分成单独的文件。在 app\Http\Routes\
.
我需要 RouteServiceProvider
中的所有文件(更正:有效..)对我来说非常好。毕竟我想用 php artisan route:cache
缓存路由。然后,如果你转到一个页面,你得到的只是一个 404 错误。
“长话短说”:新路由逻辑在 artisan 路由缓存后崩溃。
这是我在 RouteServiceProvider
中的地图函数(灵感来自 this 答案):
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();
}
}
});
}
有人知道问题出在哪里吗?或者如果我想使用路由缓存,我是否只需要将所有内容放回 routes.php 中。提前致谢。
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
文件夹中添加了多少文件,其中的 none 已注册。
所以我决定尝试使用 "require" 而不是 "require_once"。
瞧!
路线数量适当增加。
至于为什么会这样,我猜是因为作曲家自动加载(这只是一个有根据的猜测)。 看看 composer.json:
"psr-4": {
"App\": "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;
}
});