An indication of good software design is how modular and maintainable your code is. Grouping several pieces of code into one logical module that can be reused is called a “package” in Laravel.
And today, we’ll take a look at creating our very own package in Laravel 5.6 from scratch. Don’t let this shake you. Creating a package is not as complicated as it seems. There are a few simple steps using which you can create your own package.
Of course, there isn’t a recipe to create a generic package, so as an example, let’s try and create a “To Do List” package.
Through the course of this example, we’ll cover concepts like migrations, routes, views, and dependencies on other packages, and so on.
Ready to get started? Here we go!
Step #1: Install Laravel 5.6
Install the latest Laravel version i.e. Laravel 5.6. To do so go to the project directory and run the command:
The detailed Laravel documentation has you covered for any other options too.
Step #2: Create a Folder Structure
Next, we move on to create a packages folder in our project directory. The convention to name packages is as follows [Creator or Vendor]/[Package name].
For example, the “laravel/framework” package has the vendor as “laravel” and package name is “framework“.
Another popular package is “laravelcollective/html” package, here the vendor is “laravelcollective” and the package name is “html“.
Similarly, let’s name our package “wisdmlabs/todolist“. Create this folder inside the “packages” folder as:
We need to create an “src” folder under the package folder.
Step #3: Create the Composer File
Every package should have a “composer.json” file, which will contain all the packages and their dependencies. Inside the “todolist” folder run the following command
You will be prompted for details about the package. You can skip by pressing enter and it will intake the default values. You can change this information later in the “composer.json” file.
Step #4: Load the Package from the Main Composer.JSON File
Now, the “composer.json” file for every Laravel application is present in the root directory. We need to make our package visible to the application.
Add the namespace of our package in “autoload > psr-4”
Step #5: Create a Service Provider for Package
A service provider is the access point to our package. It is the class which maintains all the information regarding our package such as the location from which views, migration, routes are loaded. You may read more about service providers in the official Laravel documentation.
Lets use the artisan command to create a service provider. Run the command:
It will generate a file “TodolistServiceProvider.php” which can be found under the “app/Providers” directory. Now move that file into our package i.e., in packages/wisdmlabs/todolist/scr folder. And don’t forget to change the namespace of the file to “wisdmlabs\todolist“.
In the file you’ll notice two functions boot() and register(). The boot() function is used to initialize some routes or add an event listener. The register() function is used to bind our package to the classes inside the app container.
But before we boot or register our package, we need to our service provider in the file “config/app.php“.
Step #6: Create the Migration
Moving on, we first need to create the migration using the following artisan command:
The migration is created in this location “database/migration/*_create_task_table.php”. We will move this migration file into our package to “providers/wisdmlabs/todolist/src/migrations/*_create_task_table.php“.
Now, we can modify this migration file add the columns for our table.
Step #7: Create the Model for the Table
Once the above is done, we’ll create an eloquent model for our table so that we can use eloquent methods to easily create, update and delete the data in the table.
Run the following artisan command:
Now, move the “Task.php” file from app/Task.php to our package folder providers/wisdmlabs/todolist/src/Task.php. And again, don’t forget to change the namespace of the file to “wisdmlabs\todolist”.
Now add the necessary information in the model (Task.php) as follows.
Step #8: Create a Controller
Let’s create the controller by running the artisan command:
Next, move the controller (TaskController) from app/controllers/TaskController.php to providers/wisdmlabs/todolist/TaskController.php and change the namespace to “wisdmlabs\todolist“.
Our controller will have the necessary methods for our to-do-list package. This is the primary file and will contain all our logic.
Step #9: Create a Routes File
Create a new file in “wisdmlabs/todolist/src” folder and give the name “routes.php”. Define the routes we are going to use in our package.
Step #10: Create the Views
To create views, we have to create a “views” folder under “wisdmlabs/todolist/src/. Now, create a file for each required view under this folder.
We’ll be creating two views: 1) app.blade – for each to-do and 2) list.blade – for the to-do list.
Content to be added under app.blade.php:
Add the following under list.blade.php:
Step #11: Update the Service Provider to Load the Package
We’ve come to the penultimate step – loading the routes, migrations, views, and so on. If you want a user of your package to be able to edit the views, then you can publish the views in the service provider.
Now you can publish the views by the artisan command:
The above command will create the folder of your package under the views folder “/resources/views/wissdmlabs/todolist/”. Now, a user can change the view of the screen.
Step #12: Updating the Composer File
In the final step, we have include the “laravelcollective/html package” in our package. To do this, we have to add the dependencies of our package in “composer.json”
We can add extra object in the “composer.json” which will load our package so that a user doesn’t have to add our package in “config/app.php” in providers array. And the package will be loaded automatically.
There you go! Your package is created and all set to be used. These steps can be followed to create such packages. Questions?