How To Make Packages In Laravel

How To Make Packages In Laravel

An introduction to packages and how to implement them on Laravel

Kumar Shivam's photo
Kumar Shivam
·Aug 29, 2022·

3 min read

Play this article

Table of contents

  • Let’s create a Laravel package from scratch
  • Conclusion

Packages are standalone source code which can be imported directly to our project using package management tools. Instead of copying and pasting the source code from one project to another, we can directly import a package to our project along with the class and functionality to be included in the project.

When we are working on multiple projects, we have lots of features and functionalities which are similar in all the projects. Instead of copying and pasting the source code again and again in multiple projects, we can make our very own package and include all the features and functionalities by simply importing the package.

In PHP, we have Composer, a dependency management tool, which will manage our project by pulling all the required dependencies. Let us learn how to implement it in this article:

Let’s create a Laravel package from scratch

Follow the steps given below to create your very own package:

Step 1: Create a folder structure in your Laravel project by going to packages -> testpackage -> helloworld -> src. Refer to the image given below:

image.png

Step 2: Once the folder is created, open your terminal and go to the helloworld folder as executed below:

cd packages/testpackage/helloworld

Step 3:

Once you are in the helloworld folder, run composer init when the prompt is shown. The command will ask you some information about the package. You can refer to the image given below for clarity:

p1.png

Step 4: Once we complete the above step, it will create a vendor folder along with a composer.json file in the helloworld folder as referenced by the image given below:

p2.png

The composer.json file should like the image given below:

p3.png

Step 5: Now we can include the path in the project composer.json file so that it can be auto loaded. Run composer dump-autoload to autoload the file.

image.png

Step 6: Now, let’s create a Hello.php file in the src folder, to test our package. We can add all the features and functionality required in this file. Refer to the image given below:

image.png

Step 7: In this step, let’s create a route to check the function of our packages:

use Testpackage\Helloworld\Hello;
Route::get('/hello', function() {
   $hello = new Hello();
   return $hello->hello();
});

After running the above snippet, we will receive “Hello World!” in the browser.

Screenshot 2022-04-10 at 8.31.15 PM.png

Step 8: We can now upload the project in the git repository to be used whenever required by importing it to the project.

Conclusion

In this article, you learnt what packages are and the various advantages that it brings to Laravel coding. By following this step-by-step guide you can create and deploy your own packages when you are working with Laravel.

I hope you found this article insightful and hope to see you in my later blogs.

 
Share this