Developing a Laravel specific package

Posted by Mladjo on February 02, 2020 · 2 mins read

A PHP package is a piece of reusable code that can be dropped into any application. Some of those packages are specifically intended for use with Laravel. This Laravel Package Boilerplate generator helps you with the boilerplate code for your Laravel specific package. Create folder packages under the root of your Laravel application and place downloaded code in it.

Generated Laravel package folder structure:

Laravel Package Folder Structure

Load the Package

Load your package locally, by defining a custom repository in the composer.json file of your Laravel application:

"repositories": [
    {
        "type": "path",
        "url": "./packages/mladjom/boilerplate"
    }
]

Require your local package in the root of your Laravel application:

composer require mladjom/boilerplate

Managing Dependencies

With Laravel it is easy to use composer to bundle dependencies in your local package.

This is composer.json within custom package:

"require": {
    "php": "^7.1",
    "illuminate/support": "^6.0",
    "spatie/laravel-permission": "^3.0"
},

Update composer from main app:

composer update

In the register() method of the custom packages service provider register new service provider:

<?php

namespace Mladjom\Boilerplate;

use Illuminate\Support\ServiceProvider;
use Spatie\Permission\PermissionServiceProvider;

class BoilerplateServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     */
    public function register()
    {
        $this->app->register(PermissionServiceProvider::class);
    }
}

Loading migrations

TODO seeds

Loading routes