Arn*_*rdt 5 php caching laravel laravel-artisan
我正在使用map中的函数来RouteServiceProvider操作一些路线,然后再进行进一步处理。当我在本地计算机上运行时,一切运行正常,但在生产服务器上,由于某种原因,没有调用任何地图函数。为了确保该错误不是出于某种原因出现在我自己的代码中,我使用了原始的 RouteServiceProvider.php,仅添加了一些回声以用于测试目的:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
echo 'RouteServiceProvider boot';
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
echo 'RouteServiceProvider map';
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
echo 'RouteServiceProvider mapWebRoutes';
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
echo 'RouteServiceProvider mapApiRoutes';
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Run Code Online (Sandbox Code Playgroud)
在生产服务器上运行时我得到:
RouteServiceProvider boot
Run Code Online (Sandbox Code Playgroud)
在本地机器上运行时:
RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutes
Run Code Online (Sandbox Code Playgroud)
因此,在生产服务器上,类似乎已完美加载,并且该boot函数也被调用,但没有任何map函数被调用。我尝试清除每种类型的缓存,但结果仍然相同。然而,在清除缓存期间,它确实调用了所有地图函数:
php artisan route:cache
RouteServiceProvider bootRoute cache cleared!
RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutesRoutes cached successfully!
Run Code Online (Sandbox Code Playgroud)
知道可能导致此问题的原因或如何解决吗?
PS 在生产服务器上,所有内容都是使用 PHP Deployer 部署的,但其他一切都运行良好,所以我认为这不是问题。
如果您查看框架默认的 RouteServiceProvider(不是您的应用程序扩展的),您将看到:
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->setRootControllerNamespace();
if ($this->routesAreCached()) {
$this->loadCachedRoutes();
} else {
$this->loadRoutes();
$this->app->booted(function () {
$this->app['router']->getRoutes()->refreshNameLookups();
$this->app['router']->getRoutes()->refreshActionLookups();
});
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,if ($this->routesAreCached()) {路由是从缓存加载的,并且$this->loadRoutes();最终调用mapRouteServiceProvider 的函数。
如果您php artisan route:clear这样做,将阻止从缓存加载路由,并且您的地图方法将在每个请求上调用。
| 归档时间: |
|
| 查看次数: |
3825 次 |
| 最近记录: |