How To Make Custom Laravel Package Using Composer?
In our websites, we are integrating many functionalities. Most of the functionalities are already integrated into other websites. Now, if we want that same functionality in our website, then we will make it manually. And that process may take the same or more time as that functionality integrated in other websites. But what if we make one functionality only one time, and reuse it in other websites? To solve that, we are using laravel packages. By using Laravel packages, we can integrate already created functionalities to our website. Let’s see how to make custom laravel package using composer?
We can also make our custom laravel package for reuse. In this blog, we will learn to make our custom laravel package. You need to follow below steps:
- In the project’s root folder, create a new folder with the name: “packages”
- in the “packages” folder, create a new folder with the name of your vendor. You can put any name here as your choice. (here, I put the vendor as “jp”. Here, jp stands for jay panchal [my name]).
- In that newly created vendor folder, create a folder with your package name. (here, my package name is “slugify”).
- And finally, create a folder inside that package folder with the name: “src”.
- So, our final folder structure will look like this:
- Now, open the terminal (or cmd) and go to the package folder using cd command. In my case, i will run “cd /example-app/packages/jp/slugify/” to do that.
- Run this command: “composer init”. By running this command, cmd will ask for basic plugin details to make a
composer.json file.
(example: Package name, Author, Minimum Stability, Package Type, etc.) - You can skip the step using typing “n” and pressing enter. here, my cmd outputs are looking like this:
- After successfully passing the above step,
composer.json
file will be created to your package’s folder and you can also edit that file if you wish. - Run the below command to reload our composer file
“composer dump-autoload” - Create one class in your package’s src folder. (We should use the class name the same as our package name).
- Put the below code in that class file (You need to replace namespace with your composer’s namespace, and class with your package name in titleCase):
<?php
namespace JP\Slugify;
use Illuminate\Support\Str;
class Slugify
{
public function slug_converter(String $title)
{
return Str::slug($title);
}
}
- Our custom laravel package is ready to test. Please put the below code in your
web.php
file (replace “P\Slugify\Slugify” with your class namespace path):
use JP\Slugify\Slugify;
Route::get('/slug_converter/{title}', function($title) {
$oGreetr = new Slugify();
return $oGreetr->slug_converter($title);
});
- Run laravel using “php artisan serve”. Finally, run this URL in your browser:
http://localhost:8000/slug_converter/Jay%20Panchal
. - You can see the output like this:
- We can also upload this package to packagist site for public use. I will make the next blog for it. Thanks.