davidxd33
4 years ago
585
30
Tips

Developing your packages in Laravel 5

Posted 4 years ago by davidxd33

I spent all day trying to get my packages flow going and after intense research and tests, I came up with a nice flexible way to develop packages without illuminate/workbench.

Setup your home for making packages (your workbench).


I created a directory called "packages" in my application root. Inside of the packages directory, follow the basic structure of any package (Vendor/Package/src).

It should look like so:

packages /
    Vendor /
        Package /
            src/

Setup your composer.json


Navigate to your package directory (Vendor/Package) and run:

composer init

It will ask a series of questions to generate your composer.json. You may include any required dependencies as you wish, we'll talk about autoloading them later.

Create your PackageServiceProvider


Like any old package, create your PackageServiceProvider inside of the src folder of your package. For more information on that, refer to the docs.

Autoload your package with PSR-4


Back to your project root, edit your composer.json to autoload your package namespace.

This is what your autoload section of composer.json should look like:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Vendor\\Package\\": "packages/Vendor/Package/src"
    }
},

Create an application PackageServiceProvider


Here's the part that was tricky for me. When I wanted to use a third party in my package, I was getting a class not found exception. It was strange because I've already done my composer update. It's when I noticed that illuminate/workbench actually was requiring the composer autoload.php for all your packages. Now we're responsible for this.

Use a service provider to autoload all packages for you. Run:

php artisan make:provider PackageServiceProvider

Here's my provider (snipped for space): http://laravel.io/bin/RE24X

If you would like to skip making the PackageServiceProvider, you can manually require to autoload.php file from your package's boot() method.

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    require __DIR__ . '/../vendor/autoload.php';
}

Add your providers into your app config


Finally like all providers, add them to your config/app.php.

'providers' =>  [
    /*
    * Application Service Providers
    */
    'App\Providers\AppServiceProvider',
    'App\Providers\BusServiceProvider',
    'App\Providers\ConfigServiceProvider',
    'App\Providers\EventServiceProvider',
    'App\Providers\RouteServiceProvider',
    'App\Providers\RepositoryServiceProvider',

    'App\Providers\PackageServiceProvider',

    /*
    * Package Service Providers
    */
    'Vendor\Package\PackageServiceProvider',
]

That should be it. You now can develop your packages freely in laravel 5. If you encounter any errors or have a better solution, please tell me as I found this to be an easy way to develop packages in laravel 5.

Sources:

Please sign in or create an account to participate in this conversation.