Support the ongoing development of Laravel.io →
IOC Architecture
Last updated 1 year ago.
0

Hi Luca, you should use boot() to register some files you want to include with your package, for example filters, routes, events, etc.

It's called before the register() method, when you add the ServiceProvider to the list within app.php

The following example assumes your files are in the same dir of the ServiceProvider:

public function boot()
{
	$this->package('vendor/package');

	include __DIR__ . '/filters.php';

	include __DIR__ . '/routes.php';

	include __DIR__ . '/events.php';
}
Last updated 1 year ago.
0

I'm not sure what you mean by "only called when the service is registered into the app.php" --- it is not called once, it is called with every request (if it's listed in app.php or registered elsewhere).

Read about boot() here (2nd and 3rd paragraphs):

http://laravel.com/docs/packages#service-providers

Last updated 1 year ago.
0

I show you an example of what I mean. The first ServiceProvider is registered into the app.php file, and the boot method is called every request. The othere two are instantiated into the register method of the first one, the register() method is called manually but the boot() method on those is never called. The question is if it's correct to do something like this? is there another method to register a service provider the code?

use Illuminate\Support\ServiceProvider;
use Core\Blog\Repo\RepoServiceProvider;
use Core\Blog\Service\Form\FormServiceProvider;

class BlogServiceProvider extends ServiceProvider {
    
    /**
     * Register blog provider
     */
    public function register() {
        $repoProvider = new RepoServiceProvider($this->app);
        $repoProvider->register();

        $formServiceProvider = new FormServiceProvider($this->app);
        $formServiceProvider->register();
    }

    public function boot(){
    //... some code
    }

}

and below other two serviceProvider


class RepoServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        $app = $this->app;

        $app->bind('Core\Blog\Repo\Article\ArticleInterface', function($app) {
            $article = new EloquentArticle(
                    new Article, $app->make('Core\Blog\Repo\Tag\TagInterface')
            );
       //..... other code
        
    }
    public function boot(){
       //... some code
    }
}

class FormServiceProvider extends ServiceProvider {

    /**
     * Register the binding
     *
     * @return void
     */
    public function register() {
        $app = $this->app;

        $app->bind('Core\Blog\Service\Form\Article\ArticleForm', function($app) {
            return new ArticleForm(
                    new ArticleFormLaravelValidator($app['validator']), 
                        $app->make('Core\Blog\Repo\Article\ArticleInterface')
            );
        });
    }
    
    public function boot(){
     //... some code
    }

}

Thank you.

Last updated 1 year ago.
0

You don't want to register service providers from within service providers. Simply register them all in app/config/app.php and let Laravel do the heavy lifting for you. That way your service providers will automatically get called and you have less code that you have to maintain yourself.

Last updated 1 year ago.
0

cerbero90 said:

Hi Luca, you should use boot() to register some files you want to include with your package, for example filters, routes, events, etc.

It's called before the register() method, when you add the ServiceProvider to the list within app.php

The following example assumes your files are in the same dir of the ServiceProvider:

public function boot() { $this->package('vendor/package');

  include __DIR__ . '/filters.php';

  include __DIR__ . '/routes.php';

  include __DIR__ . '/events.php';

}

Actually register() is called before boot()

Last updated 1 year ago.
0

any update for this?

is code from luca83 right to register other service provider from our own service provider?

thanks

Last updated 1 year ago.
0

ghprod said:

any update for this?

is code from luca83 right to register other service provider from our own service provider?

thanks

No! if you want to register other service providers at runtime you need to use the app->register() method

$this->app->register( new MyServiceProvider() );

// or

$this->app->register( 'App\Providers\MyServiceProvider' );
0

Sign in to participate in this thread!

Eventy

Your banner here too?

luca83 luca83 Joined 27 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.