Laravel packages are divided into 2 types:
1. Packages for a specific framework such as a Laravel package that offers 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 configuration (discussed later) specifically designed to enhance your Laravel application.
The key advantages of Laravel packages:
- Provides 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
- Allows you to create a package for code that you can use frequently to both save resources and improve the performance of your application.
The most popular Laravel packages:
- Sayakb. The 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 together but separate from your main application. In such cases, it makes sense to extract the parts into a package. Packages or “libraries” provide an easy way to add additional functionality to existing applications and focus on one function.
You can read more about Laravel providers in our previous article.
The best way to develop a Laravel package is to put it in your Laravel application as if were an existing package. You can do it via composer.json repositories in your app’s composer.json fetching the predefined local package as a dependency.
Among the key resources available for creating a Laravel package are:
- Package Configuration
Some Laravel packages may require configuration files to be defined as typical application configuration files.
The package configuration has three parts.
How to access package configuration files
When using the default resource registration method in your service provider, you can access the package configuration files using the usual:
- 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 directly access the options without specifying the file name:
How to cascade configuration files?
If your package is installed by other developers, you can override some of the configuration options. Here you need to use the config: publish artisan command –
php artisan config:publish vendor/package
After running this command, all your configuration files for the application will be copied to app / config / packages / vendor / package, where the developer can safely modify them.
- Package Routes
Unlike earlier versions of Laravel, where the handles clause was used to specify which URIs a package can respond to, in Laravel 4 a package can respond to any URI. To be able to load the routes file for your Laravel package, you just need to include it from your service provider’s load method using:
public function boot()
You can define and use the routes similar to a normal application.
- Package Translations
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 views of the package to the app / views / packages directory, which you can publish to easily customize them to your liking.
- Package Commands
To register any command in your package, you can use a dedicated process in the boot() method.
From now on, you can use commands from the command line UI 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
Add everything to tracking and commit your code:
# add everything
# commit to git
git commit -m “commit message here”
- Push to your remote repo by typing git push origin master.
- Go to Packagist, copy your repository url and upload it.
- In your GitHub repository, go to the settings tab and look for packages.
- Fill out the form and add the service.
- Click on Test Service and your Laravel package will run.
While we’ve explored some of the basics of Laravel package development in this blog, there are a number of other possibilities to implement whatever you want, including extending the answer and having to schedule tasks or commands from your package.
We would like to write about them in our next articles in our blog. Follow us
Don't want to miss anything?
Get weekly updates on the newest design stories, case studies and tips right in your mailbox.
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 which, 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