首页 > 解决方案 > 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 部署的,但其他一切都运行良好,所以我认为这不是问题。

标签: phplaravelcachinglaravel-artisan

解决方案


如果您查看框架默认的 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,它将停止从缓存加载路线,并且您的地图方法将在每个请求上被调用。


推荐阅读