php - Laravel RouteServiceProvider 地图功能未调用
问题描述
我正在使用中的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'));
}
}
在生产服务器上运行时,我得到:
RouteServiceProvider boot
在本地机器上运行时:
RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutes
因此,在生产服务器上,该类似乎已完美加载,并且该boot
函数也被调用,但没有一个map
函数被调用。我已经尝试清除每种类型的缓存,但结果保持不变。但是,在缓存清除期间,它确实调用了所有映射函数:
php artisan route:cache
RouteServiceProvider bootRoute cache cleared!
RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutesRoutes cached successfully!
知道可能是什么原因或如何解决它吗?
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();
});
}
}
如您所见if ($this->routesAreCached()) {
,路由是从缓存中加载的,$this->loadRoutes();
最终调用的map
是 RouteServiceProvider 的函数。
如果您这样做php artisan route:clear
,它将停止从缓存加载路线,并且您的地图方法将在每个请求上被调用。
推荐阅读
- ubuntu-20.04 - 使用 polkit 在 cockpit 中设置具有特定权限的用户
- tensorflow - 我的输入和输出是单个还是多个(对于 TensorFlow 模型)?
- javascript - 如何在没有 html 标记的 Tesnsorflow JS 对象检测中为 model.detect 发送图像
- reactjs - 如何使用react-spring从右到左反向创建运动图像?
- arduino - DHT11返回数据查询
- botframework - 如何实现子串功能?
- python - matplotlib plot 圆形日周期图(每日极坐标图)
- c# - 如何在 .Net Core 中对 ADF PipeLineRun 进行单元测试?
- regex - 将行的最后一个单词转移到记事本++中的新行
- r - 在 R Shiny 中通过 bsbutton 插入 UI。单击按钮时,UI 不会插入。请问有什么帮助吗?