What Are Laravel Packages?

As a PHP developer, you may be interested in working with frameworks. Frameworks attempt to make the process of development easier by simplifying common practices used in the majority of developing web projects such as packages, modules, plug-ins, and even components (CakePHP).

Perhaps during the development process, you feel you are reinventing the wheel, such as when creating an Authentication or Captcha application. In this case, you need to create a package which will simplify and make your app development faster and easier.

There are two types of packages; some are framework independent (stand-alone) and the others are for a specific framework. This tutorial will exclusively cover Laravel packages.

Before creating a package you need to know about package managers. PEAR is a well-known PHP package manager which, although available, is rarely used. Why? It forced you to install packages system-wide, rather than on a project-by-project basis. Composer became PEAR’s successor.

What Are Laravel Packages?

One of the parameters for scaling framework power is how the developer redistributes useful packages of code. This allows developers to summarize applications into several packages of smaller applications.

Indeed, packages offer a great way to group related code. Packages are very similar to “Components”. It is important to note that the Laravel source code is a “package”, which Taylor Otwell calls the DEFAULT_BUNDLE.

One great advantage of Laravel packages is that they allow access to all the features the framework offers to its host application, including routing, migrations, tests, views, and numerous other useful features. Another important advantage of a package is the DRY (Don’t Repeat Yourself) principle. By creating a package for code you frequently use, you save resources and improve your application’s performance.

Now let’s introduce some useful Laravel packages. Packages I have used include:

  • Generators: One of the things I liked about CakePHP was its Bake feature. This is the closest thing I’ve found in any other framework.
  • Ardent: Self-validating smart models for Eloquent ORM.
  • IDE helper: This package seeks to rectify the code auto-completion issue in PhpStorm by creating a docblock helper file from which the IDE can take cues.
  • Sayakb: It brings you a great captcha app.
  • Behat: A solution for testing your application using the BDD methodology.
  • Artdarek: A great OAuth wrapper.
  • Mcamara: Easy i18n localization for your Laravel app.

You may find the packaging capability you need in packalyst. Packalyst creates a simple and social packages registry for Laravel.

How to Build Your Own Laravel Package

Create a simple authentication package for Laravel 5.0. To do this, first of all you need a composer.json file for your new package:

The Service Provider class is simply the file which sets everything up correctly for the package. Here is what the Service Provider should look like at this point, in src/MyAuthServiceProvider.php:

Add the service provider to config/app.php:

Now Laravel is aware of the package. You may ask about “alias” and why I don’t add it to my app.php. Personally, I add alias in the register method of the service provider class instead of adding it manually to the Laravel config file. I will address this later. Now create a simple PHP class called MyAuth in src/MyAuth.php:

The authentication class should bind with Laravel’s IoC Container to the service provider of the package. Before that, create a Facade class which will allow use of class methods without needing to create a new instance, as well as other benefits which were indicated earlier. Just as the MyAuth class created the new directory Facades in the Facades directory, create a new PHP class and name it src/MyAuthFacade.php:

After this, the only thing remaining is to bootstrap the package. Mine looks like:

As you can see, I have bound the Myauth class with the IoC Container. Now it is easy use the MyAuth class:

This should generate the message, Welcome log-in succeeded. If desired, you can now register your package in Packagist. The MyAuth package has now been registered and can easily be included in Laravel via the composer command: composer require "alireza/myauth".


If you’re looking for an example of a popular, mature Laravel package, then you can check out TerranetAdmin for Laravel.

This was a short overview of how to create a Laravel 5 package. As you can see, creating the package is easy, and could be an important tool for simplifying and speeding up the development process.



Related Articles