Laravel 4 / 5 Package Development

[norebro_social_networks icon_layout=”boxed”]

Hi ,
It’s time for Package development in Laravel 5. It also works with Laravel 4. Make sure you setup your fresh copy of laravel 5 using below command. It will install laravel in the current directory

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

If you open your routes file (app/Http/routes.php) you will see we are no more using Route class we can easily register routes using $router->get(). Now create a new workbench but before we do that we need to enter some details into config/workbench.php. Enter your name, email and save the file. We can now create a new workbench using below command

php artisan workbench KodeInfo/UserManagement

KodeInfo is the vendor name and UserManagement is the package name.
You can find more help on workbench using

php artisan help workbench

At this point, you may get an error regarding illuminate/support package that’s due to your minimum stability settings in workbench/kodeinfo/user-management/composer.json

Change minimum stability to dev and save your file

    "name": "kodeinfo/user-management",
    "description": "",
    "authors": [
            "name": "KodeInfo",
            "email": ""
    "require": {
        "php": ">=5.4.0",
        "illuminate/support": "5.0.*"
    "autoload": {
        "psr-0": {
            "Kodeinfo\\UserManagement": "src/"
    "minimum-stability": "dev"

If you go to your src directory in your package you can see laravel have provided us with default structure and provider which will be used to register our package. You can register your package in register method of service provider.

<?php namespace Kodeinfo\UserManagement;
use Illuminate\Support\ServiceProvider;
class UserManagementServiceProvider extends ServiceProvider {
     * Indicates if loading of the provider is deferred.
     * @var bool
    protected $defer = false;
     * Register the service provider.
     * @return void
    public function register()
     * Get the services provided by the provider.
     * @return array
    public function provides()
        return array();

Now Create a new class name it UserManagement in your src directory of package, make sure you namespace your class. We have created method hi which will return message which we will set in our configuration file shortly

<?php namespace Kodeinfo\UserManagement;
use Illuminate\Support\Facades\Config;
class UserManagement {
    public function hi(){
        return Config::get("user-management::message");

In above code we are using Config::get(‘user-management::message’) where user-management is the package name and message is the key. We can use this method when we have only one config file if we have more config file then this will help you Config::get(‘user-management::myconfig.message’)
Now create a new directory inside src and name it config. Create a new configuration file config.php and return our configuration’s.

return [
    "message" => "Welcome to your new package"

We are almost done  we have to add our new service provider to service providers array in config/app.php


Add an Alias so we can refer our UserManagement class as UserManager instead of KodeInfo/UserManagement/UserManagement .

'UserManager'      => 'Kodeinfo\UserManagement\UserManagement',

Now when you call hi method from HomeController it will return you value with message key from our configuration file.

<?php namespace App\Http\Controllers;
use UserManager;
class HomeController {
    public function index(UserManager $userManager)
        return $userManager->hi();

You can publish config file to your config folder so you can make changes. It is easy just navigate to your package and run below command

php artisan publish:config --path="workbench/kodeinfo/user-management/src/config" kodeinfo/user-management

It will copy a config.php file from package to config/kodeinfo/user-management , now if you make changes to message key in the config file it will be reflected and you don’t need to change the config in your package
Now if you want to upload the package to GitHub and submit it to packagist then follow this steps
Navigate to workbench/kodeinfo/user-management and initialize a new repo

git init

Add all files

git add -A

Now git commit with the message as ‘Initial Commit’

git commit -m 'Initial Commit'

Now go to GitHub create a new repo and enter the commands in ‘Push to Existing Repository’.
Once your repo is up and running you can submit it as a package to packagist . Go to packagist and register/ login, Click on Submit Package, Enter your repository URL and click Check if they are a package with the same namespace you will get an error if not it will succeed. Now you need to register a GitHub service hook so that your package will be updated instantly whenever you push to GitHub .
To add a service hook go to GitHub -> your repo -> settings and click on service hook, search for packagist , add your API token and save.
Now you can directly pull your package from packagist using composer.


Comments to: Laravel 4 / 5 Package Development

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.

    Stay connected




    Recent Comments

      Recent Posts

      Recent Comments