caffeinated / modules

:package: Modules package for Laravel
https://caffeinatedpackages.com
MIT License
955 stars 230 forks source link

Module routes in ServiceProviders #84

Closed benchpilot closed 9 years ago

benchpilot commented 9 years ago

Hello,

I'm trying to use your Modules package with the Menus one. I tried to create a Menu within MenuServiceProvider (as suggested in your documentation) using named routes. After throwing me "route not defined" errors, I tried to dump a named route set in my Module route.php file:

print_r(route('admin.dashboard.view'));

and it's throwing a "route not defined" error as well. If I'm setting the route in the app/Http/routes.php file then it's working, so I believe it's not a problem in the Menu package but in the Modules package not loading the routes before calling the Service Provider boot method since php artisan route:list is showing a valid route named 'admin.dashboard.view'.

I tried playing with the loading order of Service Providers in config/app.php with no results.

I'm working on a fresh install of Laravel 5.1 with Modules, Menu, Themes packages from you.

Is there any solution to create the Menu with named routes declared in the Modules routes.php files?

Thank you in advance!

Dan

chriskonnertz commented 9 years ago

Hello Goldblum, maybe this has something to do with https://github.com/caffeinated/modules/issues/81

benchpilot commented 9 years ago

Hi,

Actually I ran an composer update today and the version I'm running in 2.1.3 so it should cover that bug fix.

I even tried to include the Module routes files in app/http/routes.php file as suggested in that bug description with no luck.

Regards, Dan

chriskonnertz commented 9 years ago

You could try my bugfix (just for the sake of finding the bug): Open Caffeinated\Modules\ModulesServiceProvider (vendor/caffeinated/modules/src/Caffeinated/Modules/ModulesServiceProvider.php ) and set $defer to false (in line 11).

Maybe you will also have to delete bootstrap/cache/services.json (= service provider cache file).

benchpilot commented 9 years ago

Thank you for your fast reply. It was actually set to false already, it was probably implemented in the last update.

Btw, this was present in version 1 also, I tried to play with this configuration with Laravel 5, but then when 5.1 was out I made a fresh install and tried this approach again to create named routes Menus from routed defined in Modules routes.php file.

I believe they're not loaded at the time of MenuServiceProvider boot since I can call the named routes from my controllers without any problem. From the MenuServiceProvider I can access the named routes defined in app/Http/routes.php but not the ones defined in the Modules.

kaidesu commented 9 years ago

You'll want to generate menus within a middleware instance - I was unaware at the time that service providers are booted first before anything else. The docs for Caffeinated Menu's have already been updated to state this (as linked above).

This shouldn't be related to the modules package, but if you continue to run into issues do let me know!

You can push module middleware to the kernel like this:

$this->app['Illuminate\Contracts\Http\Kernel']
    ->pushMiddleware('Modules\Blog\Http\Middleware\MenuMiddleware');

You may use this within your module's service provider to register the middleware to be loaded.