Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rw_lara's avatar

Event Service Provider in Package

Hi,

I have created my own package and tried adding an events service provider to handle events, so now I have two service providers in my package, my normal one and my events one.

However, when I add my new package events service provider to my config/app.php file, I just get a blank screen.

I thought it may be an issue that you can only have one service provider maybe?

Any ideas?

Thanks

0 likes
9 replies
Francismori7's avatar

Nope, not the issue, you can have as many as you wish, just as a side note, Laravel nearly has 30 when you first install.

Can you show us your event service provider?

rw_lara's avatar

@Francismori7 Please see my event service provider below

<?php namespace Rapidweb\Admin;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider {

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
            'Rapidweb\Admin\Events\AdminUserLoggedIn' => [
                    'Rapidweb\Admin\Events\Handlers\EmailAdminLoggedInConfirmation',
            ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        
        
        parent::boot($events);
        

        //
    }
    
    public function register()
    {
        //
    }

I even copied my main service provider and just changed the name a bit and I get the same problem. So maybe its a name spacing issue?

My main service provider looks like this:

<?php namespace Rapidweb\Admin;

use Illuminate\Support\ServiceProvider;

class AdminServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        
        require '/var/www/fresh-laravel-5/vendor/autoload.php';
        
        //die('hello world');
        
        //require __DIR__ . '/../vendor/autoload.php';
        require __DIR__.'/routes.php';
        
        //$this->loadViewsFrom(__DIR__.'/views', 'Secondpackage');
        
        //$this->publishes([__DIR__.'/views' => base_path('resources/views/vendor/Secondpackage'),]);
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array();
    }

}
pmall's avatar

What is this ?

public function boot()
{       
    require '/var/www/fresh-laravel-5/vendor/autoload.php';

    require __DIR__.'/routes.php';
}
2 likes
swatkins_me's avatar

ping!

I need to learn this as well. Currently, I'm having to register my events/listeners in the application EventServiceProvider - I'd prefer to do it within the package source. Is there any way to do this?

daylight's avatar

Bummer, I am looking to add events to my package as well.

d3uter's avatar
class PackageServiceProvider extends ServiceProvider{

      ...

    public function register(){

        $this->app->register(PackageEventServiceProvider::class);
    }

}
6 likes
dbf's avatar

@swatkins_me @daylight

Laravels EventServiceProvider iterates over each item defined in EventServiceProvider::$subscribe:

foreach ($this->subscribe as $subscriber) {
	Event::subscribe($subscriber);
}

If your packages uses a service provider of some kind, just use Event::subscribe to add any subscribers/listeners in the register method of the provider.

use Illuminate\Support\Facades\Event;

class MyPackageServiceProvider extends ServiceProvider {
...
	public function register() {
		Event::subscribe(MyPackageSubscriber::class);
	}
...
}

Please or to participate in this conversation.