Laravel 5.5.* Custom Packages with AutoDiscover the providers

Suresh Velusamy
sureshvel
Published in
3 min readSep 15, 2017

Laravel’s custom package’s providers will be auto-discovered only if the package present in `vendor` folder, So for that we need to make our package installable via composer itself.

For demo purpose i am gonna create a simple package , which will be gonna register provider automatically and as well as registering one simple router.

Step 1: Installing laravel

We will create a simple base laravel 5.5

project composer create-project — prefer-dist laravel/laravel lacp “5.5.*”

Step 2 : Create basic directory structure as per below screenshot

Step 4: Create composer.json inside our custom package as follows

composer.json

{
"name": "suresh/calc",
"description": "This demo for auto discover providers in laravel with custom package",
"authors": [
{
"name": "Suresh Veluamy",
"email": "sureshamk@gmail.com"
}
],
"minimum-stability": "stable",
"require": {},
"autoload": {
"psr-4": {
"Suresh\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Suresh\\Calc\\CalcServiceProvider"
]
}
}

}

Step 5 : Creating Simple Provider file

php artisan make:provider CalcServiceProvider

packages/suresh/calc/src/CalcServiceProvider.php

<?phpnamespace Suresh\Calc;use Illuminate\Support\ServiceProvider;class CalcServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
include __DIR__.'/routes.php';
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}

Step 6 : Simple route file

packages/suresh/calc/src/routes.php

<?phpRoute::get('calculator', function(){
echo 'Hello from the calculator package!';
});

Still now we created a simple package

next we need to connect this package with our laravel application’s composer.json

Step 7 :

As per above content in the above image we need to update minimum-stability as dev is must and we have to configure custom packages path.

Mainly we need to add our package in main applications composer file as follows.

“suresh/calc”: “dev-master”

composer.json

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"suresh/calc": "dev-master"
},
"require-dev": {
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability" : "dev",
"repositories": [
{
"type": "path",
"url": "./packages/suresh/calc/"
}
]
}

Almost we done , we will move to start our custom package

Step 8: running composer install

$ composer install

It will install required packages as well as our custom package too, providers also got added automatically to laravel as show below

suresh/calc is discoverd

Its output time

Output of our work

❤ Happy Coding ❤

--

--