create-custom-package-in-laravel-5

How to create a custom package in laravel 5

Hello World, Today I am going to discuss how to create a custom package in laravel 5. In this tutorial, we will create a crud package. We will discuss migrations, routes, models, controllers, views and package configuration in this tutorial. You can download the full source code of this tutorials at here.

I have divided this tutorial in the following part,

  • Package introduction
  • Package directory structure
  • Package configuration via composer
  • Load package from composer.json file
  • Create Crud Controller
  • Create Crud Model and migration
  • Create Crud Routes
  • Create Crud Views
  • Connect Package with Laravel
  • Test you package

This is advance topic in laravel you should have basic understanding of laravel. You can read my article “learn laravel from scratch, for your reference.


Package Introduction

Packages are a great way to add functionality in laravel without modifying the core structure of laravel. It is same as wordpress plugins.


Package directory structure

We are creating a crud package. Following will be the files and folder in this package.

laravel-package-directory-structure

 

learning is my vendor name and crud is my package name.


Package configuration via composer

First of all, we will create composer.json file. This file will contain our package information. Following is the example of composer.json file.

composer-json-file-for-package


Load package from composer.json file

Every laravel application contains composer.json file at root path. In this file, we will load our newly created package via composer.json details. Add the following code in autoload > psr-4 definition like following.

composer-json-file-for-laravel-custom-package


Create CrudController

  • Go to root folder and enter the following command,
php artisan make:controller CrudController
  • Copy app/Http/Controllers/CrudController.php and paste packages/learning/crud/src/CrudController.php
  • Please change the namespace in this file

Create Crud Model and migration

  • Go to root folder and enter the following command to create crud model and migration
php artisan make:model Crud -m
  • Copy app/Crud.php and paste packages/learning/crud/src/Crud.php
  • Open the migration file and the following code in up method.
Schema::create('cruds', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('body',500);
            $table->timestamps();
        });
// Run this command to create table
php artisan migrate

Please ensure that you have provided the database credentials in .env file


Create Routes

Create routes.php in packages/learning/crud/src/ folder and enter the following code

<?php
    Route::get('crud','Learning\Crud\CrudController@index');
    Route::get('crud/add','Learning\Crud\CrudController@create');
    Route::post('crud/add','Learning\Crud\CrudController@store');
    Route::get('crud/update/{id}','Learning\Crud\CrudController@edit');
    Route::post('crud/update/{id}','Learning\Crud\CrudController@update');
    Route::get('crud/delete/{id}','Learning\Crud\CrudController@destroy');
?>

Create Crud Views


Connect Package with Laravel

Service provider is the intermediate between your package and laravel. Enter the following command to create CrudServerProvider.

php artisan make:provider CrudServiceProvider
  • Copy app/Providers/CrudServiceProvider.php and paste packages/learning/crud/src/CrudServiceProvider.php. Enter the following code in it.
  • <?php
    
    namespace Learning\Crud;
    
    use Illuminate\Support\ServiceProvider;
    
    class CrudServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap the application services.
         *
         * @return void
         */
        public function boot()
        {
            include __DIR__.'/routes.php';
        }
    
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->make('Learning\Crud\CrudController');
            $this->loadViewsFrom(__DIR__.'/views','crud');
        }
    }
  • Add CrudServiceProvider in your main config/app.php file in providers array like following.

laravel-service-provider

  • Run the following code to update laravel about your package.
composer dump-autoload

Test your package

Open Project in browser and enter the following path in browser url bar. Follow the links, you will see the relative results.


I hope you enjoying my tutorials, Please add your feedback in the comment section. Thank you. 🙂 🙂

Leave a Reply

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