BGWeb
158
3
Laravel

Service provider routes not working

Posted 4 months ago by BGWeb

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\[email protected]                   | auth:web   |
|        | DELETE        | locations                               | locations.onDelete                | Modules\Location\Controllers\[email protected]                | auth:web   |
|        | GET|HEAD      | locations                               | locations.index                   | Modules\Location\Controllers\[email protected]                   | auth:web   |
|        | POST          | locations/create                        | locations.onCreate                | Modules\Location\Controllers\[email protected]                | auth:web   |
|        | GET|HEAD      | locations/create                        | locations.create                  | Modules\Location\Controllers\[email protected]                  | auth:web   |
|        | DELETE        | locations/{location}                    | locations.destroy                 | Modules\Location\Controllers\[email protected]                 | auth:web   |
|        | GET|HEAD      | locations/{location}                    | locations.show                    | Modules\Location\Controllers\[email protected]                    | auth:web   |
|        | PUT|PATCH     | locations/{location}                    | locations.update                  | Modules\Location\Controllers\[email protected]                  | auth:web   |
|        | GET|HEAD      | locations/{location}/edit               | locations.edit                    | Modules\Location\Controllers\[email protected]                    | auth:web   |
|        | POST          | locations/{user}/edit                   | locations.onSave                  | Modules\Location\Controllers\[email protected]                  | 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.

Please sign in or create an account to participate in this conversation.