Creating Laravel Package

If you already have Laravel move to Step 2

Step 1. Get Laravel

composer create-project --prefer-dist laravel/laravel .

Step 2. Create following folder structure:

mkdir -p packages/aghayevi/modulename/src

Step 3. Do the following

cd  packages/aghayev/modulename
composer init
 
This command will guide you through creating your composer.json config.
 
Package name (<vendor>/<name>) [root/modulename]: aghayevi/modulename
Description []: My package for Laravel 5.6
Author [Imran Aghayev <imran.aghayev@hotmail.co.uk.co.uk>, n to skip]: n
Minimum Stability []: dev
Package Type []: 
License []: Apache License 2.0
 
Define your dependencies.
 
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
 
{
    "name": "aghayevi/modulename",
    "description": "My package for for Laravel 5.6",
    "license": "Apache License 2.0",
    "authors": [
        {
            "name": "Imran Aghayev",
            "email": "imran.aghayev@hotmail.co.uk"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}
 
Do you confirm generation [yes]? y

Step 4. Add package to laravel’s composer.json PSR-4 autoload section

cd ../..
...
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "Aghayevi\\Modulename\\": "packages/aghayevi/modulename/src"
        }
...

Step 5. Do composer dump-autoload to regenerate autoloading classes

Leave a Reply