Main menu
WalkswithMeLaravelLaravel 5 Package Development

Laravel 5 Package Development

Laravel 5 package development is little bit tricky due to few reason. first one is Laravel 5.0 changed lot of folder structures compared to Laravel 4.2 and older version, also they removed the support of workbench concept. Due to the lack of  workbench concept there is no artisan commands available in Laravel 5.0 for developing the packages.

But this is something really important bcoz , Laravel 5.0 is a suitable version and its requirement for  PHP 5.4 or greater. When we consider a framework for developing application, the stability is more important , and Laravel 5.1 version is the first stabled and it required PHP 5.5.9,  now days (August 2015 ) most of the shared hosting run with  PHP 5.4. these things are really important for Laravel 5 package development.

If I used Laravel 4.2 or Laravel 5+ there is no issue with package development bcoz I can use workbench or packager artisan commands. When I choose Laravel 5.0 for web application development the first thing I faced is lack of artisan command for developing packages. and official documentation was not enough for making the thing in one shot.

Here I will describe two options for developing Laravel 5 package development , one is standard method and other one is depreciated way, but quite easy with artisan commands bring back our old workbench concept.

Method 1 : Preferred

Laravel 5 changed the concept of workbench to packages so , Laravel 5 the packages stays in packages folders, So our package should be in the format of Vendor-Name/PackageName inside packages folder in root.

So according to me, folder structure is like follows

laravel 5 package strurture

laravel 5 package strurture

First time you have only upto src folder , rest you can create from CLI tools. First simple access the src folder via CLI, then simply init composer for making composer.json file.


composer init

Then fill the details and you will get a composer.json some thing like below format in src folder.


{
"name": "walkswithme/users",
"description": "A simple user manager for admin panel",
"license": "MIT",
"authors": [
{
"name": "Jobin Jose",
"email": "jxx@gmail.com"
}
],
"minimum-stability": "dev",
"require": {}
}

ok now our package composer is ready to go, but you need this package in Laravel right. If it is a published package we can simply use  composer require vendor/package name from CLI, but we are on the way to develop so we can’t access our package like above, so simply access the Laravel root   composer.json and add your name space with following section.


"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Walkswithme\\Users\\": "packages/walkswithme/users/src"
}
},

Then simply run ,


composer dumpautoload
// some time it may not load your name spaces correctly so try
//composer dumpautoload -o

Now your name space get register with application , so you can create ServiceProvider and controllers for your packages.

Create Service Provider for you packages.


php artisan make:provider UsersServiceProvider

This will create a service provider at app\providers\ just cut and paste it into your Src folder, You can check here what a service provider in Laravel does.

make sure you update the namespace of the service provider according to your package.


<?php
namespace Walkswithme\Users;
use Illuminate\Support\ServiceProvider;
class UsersServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->loadViewsFrom(__DIR__.'/views', 'users');
$this->publishes([
__DIR__.'/views' => base_path('resources/views/vendor/users'),
]);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->make('Walkswithme\Users\UsersController');
include __DIR__.'/routes.php';
include __DIR__.'/events.php';
}
}

Now we can add the service provider to App config of Laravel for loading our packages. add the following lines in the providers array. in config\app.php


'Walkswithme\Users\UsersServiceProvider',

Now you need to create a controller for our package , like below.


php artisan make:controller packages/walkswithme/src/controllers/UsersController

now you have a controller at controllers folder , so add some method like


public function index()
{
$users = User::all();
return view('users::partials.userslist')->with('users', $users);
}

you can add the controller in your service provider register method like below.


public function register()
{
$this->app->make('Walkswithme\Users\UsersController');
include __DIR__.'/routes.php';
include __DIR__.'/events.php'; // this for managing menus, not need in your case
}

Now you need a route file to access this controller method.

create a route.php file under src folder of your package and add some codes like below.


<?php
Route::get('users/all', [
'name' => 'List User',
'as' => 'users.list',
'uses' => 'walkswithme\users\UsersController@index'
]);
Route::get('users/create', [
'name' => 'Add User',
'as' => 'users.add',
'uses' => 'walkswithme\users\UsersController@create'
]);
Route::get('users/edit/{id}', [
'name' => 'Edit User',
'as' => 'users.edit',
'uses' => 'walkswithme\users\UsersController@edit'
]);
Route::get('users/delete/{id}', [
'name' => 'Remove User',
'as' => 'users.remove',
'uses' => 'walkswithme\users\UsersController@destroy'
]);

We need a view file to view the data in a page, basically it retrieve all the users details from the users table, So your view will be saved in src/views/ folder. here I just add one more folder called partials. the file name will be userslist.blade.php in my case.


<div class="container">
<div class="col-md-8">
<ul class="list-group"> @foreach($users as $user)
<li class="list-group-item">{{$user->name}}</li>
@endforeach </ul>
</div>
</div>

Ok now the time to publish our package , publish doesn’t mean it to packagist, Laravel 5 introduces an artisan command.


php artisan vendor:publish

This command will copy our view and config files (If any) to resources folder and config folder, according to our package structure. Yes in Laravel 5 the views are getting saved in resources folder, It allows us to override vendor’s packages once we create a theme. here for us that path is we defined in our service provider like below in the boot method.


$this->publishes([
__DIR__.'/views' => base_path('resources/views/vendor/users'),
]);

Package Check List

  • Create entire folder structure you want in your package.
  • init the composer and set the package details.
  • Register the name space in your laravel root composer.json.
  • Create Service Provider for your package (copy to correct path).
  • Register your service provider in config\app.php.
  • Composer dumpaulload for loading name space.
  • Create controller,views, models, routes.
  • run vendor publish.

Yes, Now your custom package is ready to use , check in browser like, localhost/l5/users/all it will load all the users list.

you can download the source code from below

Download1166 downloads
Method 2: Workbench Pattern

The custom package development with Laravel 5 , using depreciated method of 4.2 workbench can be red here.

Hope you guys able to find this article is useful for Laravel 5 package development.

Thanks for reading 🙂 🙂 🙂

 

8 thoughts on “Laravel 5 Package Development

  1. Pingback: Workbench Package Development for Laravel 5 - | WalksWithMe

    1. Yes, you can simply publish your package in a public git repo. with keyword as laravel in your composer.json.

      like


      "keywords": ["laravel","other keywords"]

      Then follow the instructions here.
      You just need to publish on http://packalyst.com/ only need to mention laravel keyword in the keyword section, The http://packalyst.com/ will read all the public Git Repo that have Laravel key word.

      Hope it helps.

      1. Good read for laravel developers, Jobin!!

        But this is something really important bcoz , Laravel 5.0 is stable version and its requirement for PHP 5.4 or greater. When we consider a framework for developing application, the stability is more important , and Laravel 5+ versions are not yet stabled and it required PHP 5.5.9, now days (August 2015 ) most of the shared hosting run with PHP 5.4. these things are really important for Laravel 5 package development.

        But from the laravel site, I understand that 5.1 is their first LTS. Also regarding the PHP version, PHP 5.4 support will be no longer available. hope server guys take a note on this.

        If I used Laravel 4.2 or Laravel 5+ there is no issue with package development bcoz I can use workbench or packager artisan commands.
        I think it is true with L4.2 but L5+ doesn’t support workbench concept as they want to make packages independent of the core structure.

        From the site:
        Since PHP 5.4 will enter “end of life” in September and will no longer receive security updates from the PHP development team, Laravel 5.1 requires PHP 5.5.9 or greater. PHP 5.5.9 allows compatibility with the latest versions of popular PHP libraries such as Guzzle and the AWS SDK.
        LTS

        Laravel 5.1 is the first release of Laravel to receive long term support. Laravel 5.1 will receive bug fixes for 2 years and security fixes for 3 years. This support window is the largest ever provided for Laravel and provides stability and peace of mind for larger, enterprise clients and customers.

        1. Great Vipindas,

          thanks for the head up !

          Regarding stable versions, Will update the content,
          Laravel 5.1 have a package develop method available, with artisan command packager, that is why I mentioned like that.

Leave a Reply

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

 

FacebookTwitterGoogle+RSS