gpt4 book ai didi

php - Laravel RouteServiceProvider map 功能未调用

转载 作者:行者123 更新时间:2023-12-03 23:05:07 25 4
gpt4 key购买 nike

我正在使用 map RouteServiceProvider 中的函数在进一步处理之前操纵一些路线。
当我在本地机器上运行时,一切正常,但在生产服务器上由于某种原因没有调用 map 函数。
为了确保错误不是出于某种原因在我自己的代码中,我使用了原始的 RouteServiceProvider.php 并仅添加了一些用于测试目的的 echo :

<?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功能是。
我尝试清除每种类型的缓存,但结果保持不变。但是在缓存清除期间,它会调用所有 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它将停止从缓存加载路由,并且将在每个请求上调用您的 map 方法。

关于php - Laravel RouteServiceProvider map 功能未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63214459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com