Laravel Package Development – What Are Packages In Laravel?

Voiced by Amazon Polly

Laravel as a robust PHP development framework needs no introduction today. With the ever-growing community around Laravel, the number of resources available to its users is also increasing at a rapid pace.

In this blog, we are going to explore and discuss the available resources to help you create your own Laravel packages.

Understanding Laravel Packages

The scaling of any framework is largely dependent on how the developer redistributes useful packages of code as it allows them to summarize applications into several packages of smaller apps. 

Packages are mainly of two types –

  • Framework independent (standalone)
  • Packages for a specific framework such as the Laravel package that offer a way to group related code

Similar to components, a Laravel package is a set of reusable classes created to add more functionality to Laravel. These packages may have controllers, views, routes, and configuration (discussed later), specifically intended to enhance a Laravel application.

The key advantages of Laravel packages  –

a. Allows access to all the features that the framework offers to its host application including migrations, tests, routing, views, and several other useful features. 

b. Allows you to create a package for code that you can frequently use to both save resources and improve your app’s performance.

Popular Laravel Packages

Among some useful Laravel packages include –

  • Ardent – offers self-validating smart models for Eloquent ORM
  • Behat – A robust solution for testing your app using the BDD methodology
  • Sayakb – The package brings you a powerful captcha app
  • IDE helper: Another useful Laravel package, it seeks to rectify the code auto-completion issue by creating a docblock helper file to give cues to IDE 

Package Discovery

With Laravel 5.5 and the latest version of the composer, there is a new feature called Package Auto-Discovery which essentially means that installing packages in your PHP and Laravel apps will become much simpler.

For a developer who is creating a package, this means that they can easily add this functionality without telling their users to add the ServiceProvider file to their config/app.php. This allows for packages to auto-load or auto-discover service providers as they are being installed.

To implement this in your package, all you need to do is add –

 “extra”: { “laravel”: { “providers”: [ ]}}

inside the composer.json file of your package. 

After doing this, when users install the package, they would not need to manually add the ServiceProvider. This is an excellent feature that makes it easier to build and use packages.

Laravel Package Service Providers

One of the essential parts of a Laravel package is its Service Provider. A service provider is mainly a file that can tell the parent Laravel app about various offerings of the package. 

Laravel comes with several service providers including AuthServiceProvider, AppServiceProvider, RouteServiceProvider, and EventServiceProvider which take care of registering application-specific services (as service container bindings), routes, and event listeners.

Every service provider in the Laravel package extends the Illuminate\Support\ServiceProvider and implements a register() and a boot() method.

While the boot() method is used to bind things in the service container, the register() method is used to register a class binding in the service container, enabling a class to be resolved from the container. 

Package Resources

Package ResourcesThe best way to develop a Laravel package is to bring it into a Laravel application like it would be an existing package. You can do it via composer by definite repositories in your application’s composer.json and pulling the pre-defined local package as a dependency.

Among the key resources available to build a Laravel package include –

1. Package Configuration

Some Laravel packages may require configuration files which should be defined as typical application configuration files. 

There are three parts to package configuration –

  • How to access package configuration files

When using the default method of registering resources in your service provider, you can access package configuration files using the usual –

“double-colon” syntax-

Config::get(‘package::file.option’);

  • How to access single file package configuration

In case your Laravel package contains a single configuration file, you can name the file config.php and then access the options directly, without specifying the file name:

Config::get(‘package::option’);

  • How to cascade configuration files

In the case of other developers installing your package, you can override some of the configuration options. Here you need to use the config: publish artisan command –

php artisan config:publish vendor/package

Once you execute this command, all your configuration files for the app will be copied to app/config/packages/vendor/package where they can be modified by the developer safely. 

2. Package Migrations

Creating and running migrations for any of your Laravel packages is quite simple. 

You need to add one line to the boot() method again and it’s good to go.

$this->loadMigrationsFrom(__DIR__.’/../database/migrations’);

As soon as you run the PHP artisan migrate command, Laravel will migrate the package’s migration automatically without any extra effort.

3. Package Routes

Unlike earlier versions of Laravel where the handles clause was used to specify which URIs a package could respond to, in Laravel 4, a package may respond to any URI. To be able to load a routes file for your Laravel package, you need to simply include it from within your service provider’s boot method using –

public function boot()

{ $this->package(‘vendor/package’);

include __DIR__.’/../../routes.php’;}

You can define and use the routes similar to a normal application.

4. Package Translations

During package development, localization can be a frequent need. Similar to viewing files, you can publish and load them easily from the boot() method. To be able to load translations, you need to use –

$this->loadTranslationsFrom(__DIR__.’/../resources/lang’, ‘package-name’);

5. Package Views

When using a Laravel package in your application, you might wish to customize the package’s views. You can export the package views to your own app/views directory using the view: publish Artisan command:

PHP artisan view: publish vendor/

The above command will allow you to move the package’s views into the app/views/packages directory, post which you may easily tweak them as you want.

6. Package Commands

To register any command in your package, you can use a dedicated process in the boot() method.

$this>commands([Commands/CommandOne::class,Commands/CommandTwo::class,]);

From here on, you can use the commands from the artisan CLI with the signature you specified for the commands.

Publishing File Groups

To make your package available on the composer, you need to upload it to Packagist and update your remote repository. Here are the steps to do the same –

  • On GitHub create a new repository
  • Copy the clone link and in the terminal of your package folder enter

git remote add origin [repository link]

  • Add everything to tracking and commit your code:

     # add everything

     git add.

     # commit to git 

     git commit -m “commit message here”

  • Push to your remote repo by typing- git push origin master
  • Go to Packagist, copy the URL of your repository and submit it
  • On your GitHub repository go to the settings tab and search for packages
  • Fill the form and add the service
  • Click on test service and your Laravel package will be live

To Summarize

While we explored some of the basics of Laravel package development in this blog, there is a range of other possibilities to implement anything you want including extending response and the need to schedule jobs or commands from your package. 

You can also read the official documentation of package development here to read more information that is not covered in this blog.To make your package available on the composer, you need to upload it to Packagist and update your remote repository. Here are the steps to do the same –

Latest Post