Saturday, March 4, 2017

Laravel 5 how to modify execution of ServiceProviders

What I want to do is to load different ServiceProviders dinamically to register some Routes. If I put include $pathToModuleRoutes; inside ModuleServiceProvider it works like a charm, but I want that logic in each ServiceProvider for each module.

My app.php looks like this:

    App\Providers\AppServiceProvider::class,
    App\Providers\ModuleServiceProvider::class,                       //Mine
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Providers\ErrorServiceProvider::class,

The ModuleServiceProvider looks like this:

public function boot()
{

    if (config('modules')) {
        foreach (config('modules') as $module => $content) {

            $serviceProvider = '\Modules\\' . $module . '\Providers\ModuleServiceProvider';

            if (class_exists($serviceProvider)) {
                $this->app->register($serviceProvider);
            }
        }
    }
}

One of my ModuleServiceProvider inside a module looks like this:

public function boot()
{
    $this->loadViewsFrom(base_path() . '/modules/Blog/resources/views', 'blog');
    $this->loadTranslationsFrom(base_path() . '/modules/Blog/resources/lang', 'blog');

    include base_path() . '/modules/Blog/Http/routes.php';

}

This code (module ModuleServiceProvider) is executed, I checked with a dd(), but I think those modules are added to the end of the list of service providers. I can't use those routes and I think that's because RouteServiceProvideralready did his job.

Any idea how to achieve the behaviour I want?

EDIT: executing php artisan route:list show the routes I can't execute



via Lloople

Advertisement