Skip to content
Advertisement

Laravel RouteServiceProvider map function not called

I’m using the map function in the RouteServiceProvider to manipulate some routes before are being processed any further. When I run on my local machine everything runs fine but on the production server for some reason non of the map functions are being called. To make sure the bug was not for some reason in my own code I used the orignal RouteServiceProvider.php with just some echo’s added for testing purposes:

<?php

namespace AppProviders;

use IlluminateFoundationSupportProvidersRouteServiceProvider as ServiceProvider;
use IlluminateSupportFacadesRoute;

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 = 'AppHttpControllers';

    /**
     * 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'));
    }
}

When running on production server I get:

RouteServiceProvider boot

When running on local machine:

RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutes

So it seems on the production server the class is perfectly loaded and the boot function also is called but the none of the map function are. I’ve tried clearing every type of cache but the result remained the same. However during the cache clear it DOES call all the map functions:

php artisan route:cache
RouteServiceProvider bootRoute cache cleared!
RouteServiceProvider bootRouteServiceProvider mapRouteServiceProvider mapApiRoutesRouteServiceProvider mapWebRoutesRoutes cached successfully!

Any idea what might be causing this or how to resolve it?

PS On the production server everything is deployed using PHP Deployer, but everything else is running fine so I assume that’s not the issue.

Advertisement

Answer

If you look at the frameworks default RouteServiceProvider, (Not the one that your application extends), You will see:

/**
     * 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();
            });
        }
    }

As you can see if ($this->routesAreCached()) { then the routes are loaded from cache, and $this->loadRoutes(); is what ultimately calls the map function of your RouteServiceProvider.

If you do php artisan route:clear it will stop the routes from being loaded from cache, and your map methods will be called on each request.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement