Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When making a Laravel package, how do I register the service provider and alias of dependency packages?

I'm creating a package for Laravel and I've defined the Notification package (https://github.com/edvinaskrucas/notification) as a dependency for my package.

In /workbench/vendor/package/src/composer.json I have:

"require": {
    "php": ">=5.3.0",
    "illuminate/support": "4.1.*",
    "edvinaskrucas/notification": "2.*"
}

I'm then registering the service provider in my package's service provider's register method (not even sure if this is the right way to do this), and the alias using App::alias.

So in /workbench/vendor/package/src/Vendor/Package/PackageServiceProvider.php I have:

public function register()
{
    App::register('Krucas\Notification\NotificationServiceProvider');
    App::alias('Notification','Krucas\Notification\Facades\Notification');
}

But I'm still getting "Class 'Notification' not found" exception when attempting to use Notification::info() in a controller or Notification::showAll() in a view.

How do I properly register service providers for my package's dependencies?

like image 956
tprsn Avatar asked Feb 05 '14 10:02

tprsn


People also ask

How will you register service providers in Laravel?

Registering ProvidersAll service providers are registered in the config/app. php configuration file. This file contains a providers array where you can list the class names of your service providers. By default, a set of Laravel core service providers are listed in this array.

What is the official package dependency manager of Laravel?

In JavaScript we use NPM, i.e. the Node Package Manager. For backend, Composer is a very popular dependency manager.

How do you implement packages in Laravel?

To initialize a new Laravel custom package development, cd into the packages/fhsinchy/inspire directory and execute the composer init command. This will start the package initialization process. Make sure to write the package name, description, and author information properly.

What is service provider and service container in Laravel?

Service providers in laravel application is the central place where application is bootstrapped. That is, laravel's core services and our application's services, classes and their dependencies are injected in service container through providers.


1 Answers

During package development you should add your package service provider in composer.json file as in the code below. For additional information please consult at Laravel's Package Discovery.

"extra": {
    "laravel": {
        "providers": [
            "Barryvdh\\Debugbar\\ServiceProvider"
        ],
        "aliases": {
            "Debugbar": "Barryvdh\\Debugbar\\Facade"
        }
    }
},
like image 98
Bedram Tamang Avatar answered Oct 20 '22 00:10

Bedram Tamang