Create custom Authorization package with Laravel 5.2

Table of Content

When you develope project with Laravel 5.2, sometime you need to grab small features like authorization, backend templates, editors, … to packages, so you can use again when start new projects.

For example, day by day, i code a lot of similar projects, all about medicine products. It will be easy and fast if i can grab all the thing i added to fresh Laravel instance to a package, and easy to pull it with composer require mypackage/medicine.

In this tutorial, i will introduce you how to make a package with Laravel 5.2.30 (Latest version). You maybe see some tutorials about this topic somewhere, but as i know they are incompleted and out of date.

Step by Step to make an Authorization Package with Laravel 5.2

  • Install fresh version of Laravel 5.2 using command line :

  • Make it working by create database, change Database config in .env and run php artisan migrate.
  • From the root directory of project, create new directory call packages, assumming that your github respository for place package will be thienkimlove/vauth, you need to create packages\thienkimlove\vauth\src.
  • Add below line in root directory composer.json :

and run command composer dump-autoload in root directory. It will allow to autoload classes in our created folder packages/thienkimlove/vauth/src.

  • Go to packages/thienkimlove/vauth/src and type composer init to create composer.json there, check the example i created below :

This file will be use to declare our package with https://packagist.org and define autoload folder when using in Laravel project later.

  • Write packages components

In this example i build the Laravel authorization system base on Laravel Authorization. It is very simple thing with roles, permissions…

Components for this example will be one ServiceProvider file , the most important thing needed for a package. Let create a provider file using command line :

It will result a new created file app\Providers\VauthServiceProvider.php, move this to packages/thienkimlove/vauth/src and change its namespace to namespace Thienkimlove\Vauth;.

This provider file contains 2 functions boot and register. We will deal with them later.

  • Next where we locate some other components like : migrations, views, config, controllers, models files?
    I will create packages/thienkimlove/vauth/src/migrations folder to store migration files, views folder in same directory to store view files, Models folder to store models files, and put my custom config file vauth.php in src directory.

Those components maybe need to copy to correctly folder once user install package, or just stay in vendor directory. I need user can edit my view files if they need so i do like that in packages/thienkimlove/vauth/src/VauthServiceProvider.php :

For other components please see below :

  • In this example project, i need to run a code block in one provider file, how i move it to packages?

First we create VauthRegistrar class which do those code block.

Next we call this class function in boot method of VauthServiceProvider :

  • How about controllers?

About our package Controller, we just leave them in package directory and using namespace to call, same like models.

The last thing we need to do, is create a readme.md file, which useful to introduce user who using our package, about how to install in details.

Ved Authorization Package

Install

  1. Install fresh version of Laravel 5.2

  2. Require package composer require thienkimlove/vauth:dev-master

  3. Add below like to config/app.php :

  1. Run php artisan vendor:publish --tag="migrations"
    && php artisan migrate && php artisan make:auth

  2. Add in routes.php

Modify app\User.php :

  1. Register one user.

  2. Create permission and role and map to user.

  1. Browser to /example

You can take a look at https://packagist.org/packages/thienkimlove/vauth for complete example. If have any questions, please leave a comment.

Leave a Reply

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