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';
}
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):
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.
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.
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()
any update for this?
is code from luca83 right to register other service provider from our own service provider?
thanks
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' );
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community