As the official documentation says, a package is the main means of adding functionality in Laravel.
Laravel’s packages are divided into 2 types:
1. Packages that offer a way to group related code
2. Similar to components. A Laravel package is a collection of reusable classes created to add additional functionality to Laravel. These packages can have controllers, views, routes, and configurations specifically designed to enhance your Laravel application.
The key advantages of Laravel packages:
- Provision of access to all the functionality that the platform offers to its host application, including migrations, tests, routing, views, and a number of other useful features
- Opportunity to create a package for code that you can frequently use to save resources and improve the performance of your application.
The most popular Laravel packages:
- Sayakb – this package brings you a powerful captcha app
- Behat – a robust solution for testing your app using the BDD methodology
- Ardent – offers self-validating smart models for Eloquent ORM
- 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
The main Reasons to develop a packagе
You may run into a scenario where you want to reuse some functionality of your application in other applications, open source for specific functionality, or keep related code separated from your main application. In such cases, it makes sense to extract the parts into a package. Packages or sometimes “libraries” provide an easy way to add additional functionality to existing applications and focus on one function.
So the main reasons are:
- Comfort in use
- Widening the functionality
- Increase of an app’s performance
Creation of a package
There are dozens of methods to create a package in Laravel. But we would like to tell you about the simplest and most proven in-practice method. The rest you can discover by yourself while creating your own projects and gaining experience.
One of the most common methods to create a Laravel package is to use the workbench command in Artisan interface. The first thing you need to do is to install several parameters in your workbench file. For example, app/config/workbench.php.
Here you can find different settings like email or name. All their values will be used later for generating composer.json of your new package. Once the values are filled, the initial step is over.
The usage of the workbench command looks like that: php artisan workbench vendor/package –resources
When we applied the workbench command, your package will be in the workbench directory of Laravel. After that, you have to register ServiceProvider – a service provider that was created for your package. How to do that, you can read in our blog post – “Laravel providers services”.
After all that manipulation, you will be ready for development.
Development of a package
In order to have more freedom for experiments, it would be nice if you work in the context of your app.
To do that, just make a fresh install of Laravel and use the workbench command to create the structure of your package. After that, use git init in your workbench directory and git push to push the package to the storage. By doing this you won’t need to use composer update and that allows working easily in the app’s environment. In this way, Laravel can scan workbench directory, find the packages and download them when launching the app.
In case you need to register the files for auto-downloading, just use php artisan dump-autoload. This command will rebuild the auto-downloading files for a root app and all workbench packages that you have already created.
Sometimes to ensure the correct work of your package and not invent the wheel, you can use external files or routes to them. To do that, you need to use include from boot method of your service provider.
Setting of your package
In some cases, it is necessary to create the files with settings for your package. These files have to be identified as the settings files of a regular app. After that, you can use a standard command that will register the resources of the package $this->package(). When you do so, the settings files will be accessible through a regular syntax with double columns(::).
There are also some cases that go beyond, like the setting of a single sittings file or manual registration of resources namespace. Here we won’t disclose such peculiar details, while you can read about them in the official documentation of Laravel.
Release of your package
When your package is ready, you need to release or publish it in Packagist storage. Don’t forget about respect for others and add the tag “laravel” to the composer.json of your package. After release, you can keep working on your package if you want to improve it. Just use the workbench command one more time and keep developing in your app’s environment.
As we have said previously, Laravel provides huge possibilities and flexibility for developers. Creating your own package allows you to make the development process faster and more qualitative for you to apply your custom logic and make everything much easier for yourself. You don’t need to write or set the same things over and over. You can do it once and avoid wasting time.
We hope that now you understand how you can create your own package for a project, but remember: in order to master the skill and knowledge you have to try everything in practice. This is the only method to understand how everything works.
If you want to create something greater and beneficial but have a lack of skills or are just a business person, turn to us. Our expertise and knowledge will serve you well, and we create the product that you want.
Don't want to miss anything?
Get weekly updates on the newest design stories, case studies and tips right in your mailbox.
Laravel for non-technical professionals
Laravel is one of the most lucrative frameworks for many business projects. But not everyone knows about it. Because all the information available in IT blogs for business representatives is perceived as some kind of magic. After reading, nothing is clear. But it's not a problem. We will explain what Laravel is and when you need to choose this framework as clearly as possible.Read more