Laravel-modules: Route caching problem

Created on 7 Jan 2017  路  5Comments  路  Source: nWidart/laravel-modules

After some debugging and some measurements, I found such bug in module:
when routes are not cached, everything works good. But when cache file is generated (bootstrap/cache/routes.php), routes are included twice, from RouteServiceProvider and from each module with require line call from start.php file.

_My approach was different to include routes, but still, here those timing results:
Without cache 0.05-0.07s,
with cache 0.09-0.12s,
with fix 0.03-0.05s_

So my suggestion would be to change in
start.stub
by adding additional checking, to not include twice

if (!app()->routesAreCached()) {
    require __DIR__ . '/Http/routes.php';
}

for route-provider.stub also same suggestion.

bug

Most helpful comment

Hello & thanks for the suggestion.

This is now on dev-master, feel free to test it out.

All 5 comments

Hello & thanks for the suggestion.

This is now on dev-master, feel free to test it out.

I have noticed,that path has changed:
https://github.com/nWidart/laravel-modules/commit/628f72a03fa94a152269141682983fccf3a32c19#diff-6a8f4f00dae32987c08a4083bfdca21aR38
missing /..

This was fixed on latest release. 馃憤

This seems not carried over to latest lumen, was this removed? lumen-framework v5.6.4, this triggers errors to @nWidart package.

I know the post is old, but I'm using this package now and I had a solution, in the App\Providers\RouteServiceProvider I put it on boot function:

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            foreach (Module::getByStatus(1) as $module) {
                $namespace = "Modules\{$module->name}\Http\Controllers";

                Route::middleware('web')
                    ->namespace($namespace)
                    ->group(module_path($module->name, '/Routes/web.php'));

                $path = module_path($module->name, '/Routes/api.php');

                if (file_exists($path)) {
                    Route::prefix('api')
                        ->middleware('api')
                        ->namespace($namespace)
                        ->group($path);
                }
            }
        });
    }

This worked to me, even though I created a global ModuleServiceProvider, so my Modules doesnt need any Services Providers.

Remember that to remove the two functions mapWebRoutes and mapApiRoutes from each RouteServiceProvider from your module.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sandeepk2304 picture sandeepk2304  路  3Comments

developh picture developh  路  3Comments

pawlox picture pawlox  路  4Comments

vdjkelly picture vdjkelly  路  4Comments

cyberbaseza picture cyberbaseza  路  4Comments