BGWeb's avatar
Level 7

Service provider routes not working

I am using "module" services providers to do things like register navigation and permissions. I am also trying to move module specific routes to a routes.php in the module and load the routes using: $this->loadRoutesFrom(__DIR__ . '/routes.php'); as described by Laravel documentation

I have a ModuleServiceProvider class that dynamically loads each module's service provider:

    public function boot()
    {
        $modules = array_diff(scandir(base_path() . '/modules'), array('..', '.'));

        foreach ($modules as $module) {
            /*
             * Register paths for: config, translator, view
             */
            $modulePath = base_path() . '/modules/' . $module;

            $this->loadRoutesFrom($modulePath . '/routes.php');
            $this->loadViewsFrom($modulePath . '/views/', $module);
            $this->loadMigrationsFrom($modulePath . '/migrations');
            $this->loadTranslationsFrom($modulePath . '/lang', $module);
        }
    }

And for example, my Locations module has a routes file:

<?php

Route::middleware('auth:web')->group(function () {
    /**
     * Location Module Routes
     */
    Route::resource('locations', '\Modules\Location\Controllers\LocationController');
});

I have cleared my cache and routes, and am able to do a route:list showing the routes are "registered":

art route:list | grep locations
|        | POST          | locations                               | locations.store                   | Modules\Location\Controllers\LocationController@store                   | auth:web   |
|        | DELETE        | locations                               | locations.onDelete                | Modules\Location\Controllers\LocationController@onDelete                | auth:web   |
|        | GET|HEAD      | locations                               | locations.index                   | Modules\Location\Controllers\LocationController@index                   | auth:web   |
|        | POST          | locations/create                        | locations.onCreate                | Modules\Location\Controllers\LocationController@onCreate                | auth:web   |
|        | GET|HEAD      | locations/create                        | locations.create                  | Modules\Location\Controllers\LocationController@create                  | auth:web   |
|        | DELETE        | locations/{location}                    | locations.destroy                 | Modules\Location\Controllers\LocationController@destroy                 | auth:web   |
|        | GET|HEAD      | locations/{location}                    | locations.show                    | Modules\Location\Controllers\LocationController@show                    | auth:web   |
|        | PUT|PATCH     | locations/{location}                    | locations.update                  | Modules\Location\Controllers\LocationController@update                  | auth:web   |
|        | GET|HEAD      | locations/{location}/edit               | locations.edit                    | Modules\Location\Controllers\LocationController@edit                    | auth:web   |
|        | POST          | locations/{user}/edit                   | locations.onSave                  | Modules\Location\Controllers\LocationController@onSave                  | auth:web   |

However, when I try to access the routes, I am redirected back to my previous page. For instance, if I am at http://127.0.0.1:8000/home and I try to navigate to http://127.0.0.1:8000/locations I am simply redirected back to /home

I have tried moving the $this->loadRoutesFrom() call to, say, the AppServiceProvider and it is still not working.

Does anyone have any suggestions as to what I may be missing here?

Thanks in advance, I look forward to any advice or comments.

0 likes
3 replies
thoasty's avatar
thoasty
Best Answer
Level 3

You will probably need the whole "web" middleware group, because its middlewares are also responsible for starting the session and other stuff.

Look at

app/Providers/RouteServiceProvider.php at the mapWebRoutes() method

and at app/Http/Kernel.php

to get a better understanding.

BGWeb's avatar
Level 7

I'm not sure I follow. I have the routes wrapped with the web middleware group, just like I would in the routes/web.php file.

What's got me is that the routes are listed with route:list and no errors are thrown when referencing them in my views. However, accessing them simply redirects me back to the previous page.

Something else must be going on that I'm not seeing.

Thanks for the reply but I'm still stuck here and open to any other suggestions.

BGWeb's avatar
Level 7

@thoasty Thanks for the pointers. I was able to get this going by using the method mapWebRoutes() from app/Providers/RouteServiceProvider.php :

    protected function mapRoutes()
    {
        Route::prefix('user')
        ->middleware('web')
        ->namespace($this->namespace)
        ->group(__DIR__ . '/routes.php');
    }

Please or to participate in this conversation.