Laravel Package development

Part 1 (Local development)

1. Fresh Laravel 5.4 installation

composer create-project --prefer-dist laravel/laravel projectName "5.4.*"

2. Folder creation

Inside projectName create folders packages/packagename/src/routes.

3. Create composer.json for package.

Open your cmd inside packagename folder: and run the following command:
composer init
You have to answer few questions to generate this composer.json file. Your answer should generate a composer.json file like below: (press enter if you do not know any anser)
{
    "name": "vendorname/packagename",
    "description": "This is my first Laravel package.",
    "type": "laravel",
    "license": "MIT",
    "authors": [
        {
            "name": "Rejaul",
            "email": "rejauldu@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

4. Routes

Inside routes folder create a file web.php which contains code like below:
<?php
Route::get('/demo', function() {
return 'This is a demo route';
});

5. Creating service provider

You sholud create a service provider named: PackagenameServiceProvider.php inside src folder of the package. This service provider should contain code like below:
<?php
namespace Vendorname\Packagename;
use Illuminate\Support\ServiceProvider;
class PackagenameServiceProvider extends ServiceProvider
{
    public function boot()
    {
        require __DIR__.'/routes/web.php';
    }
    public function register()
    {
        //
    }
}

6. Loading package via main composer.json

Open the main composer.json which lies inside projectName folder and add the following line inside psr-4:
"Onbiponi\\Message\\":"packages/message/src"
The psr-4 section will look like:
"psr-4": { "App\\": "app/", "Onbiponi\\Message\\":"packages/message/src" }
psr-4 basically defines the namespace:location-of-src

7. Adding the service provider to providers array

Add the PackagenameServicePrivider to the providers array of config\app.php as like below:
Vendorname\Packagename\PackagenameServiceProvider::class,

8. Composer update

Now go to the projectName folder and run the following commands:
composer dump-autoload
or the following three commands
php artisan config:clear

php artisan clear-compiled

composer dump-autoload

9. Check if working!

Now check the url: http://example.com/demo is working or not.

Part 2 (Installation from command line)

1. Create composer.json file like below:
{
"name": "onbiponi/message",
"description": "This is my first Laravel package.",
"type": "laravel",
"license": "MIT",
"authors": [
{
"name": "Rejaul",
"email": "rejauldu@gmail.com"
}
],
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Onbiponi\\Message\\":"src/"
}
},
"minimum-stability": "dev",
"require": {
"php": ">=5.6.4",
"laravel/framework": ">=5.5.0"
},
"extra": {
"laravel": {
"providers": [
"Onbiponi\\Message\\MessageServiceProvider"
]
}
}
}


2. publishing your package to Packagist

  • You form your package as a repository and upload it to GitHub;
  • You register on Packagist.org;
  • You make sure that your composer.json contains all necessary information;
  • You submit a link to your GitHub repository to Packagist – and then – BOOM! – magic happens!
A complete project on github: https://github.com/rejauldu/message

Labels: ,

© copyright-2020 Rejaul