3

How To Make Packages In Laravel

 1 year ago
source link: https://blog.geekyants.com/how-to-make-packages-in-laravel-a8dfce8ed44
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How To Make Packages In Laravel

An introduction to packages and how to implement them on Laravel

1*a74i8mWtqpjawQOEwex_JA.jpeg

Table of Contents

  • Let’s create a Laravel package from scratch
  • Step 1
  • Step 2
  • Step 3
  • Step 4
  • Step 5
  • Step 6
  • Step 7
  • Step 8
  • 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:

0*FMl066A2nKDAw-l5

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:

0*aybBDpStcY1mzhZe

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:

0*gKqdMYMP3rV1MliR

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

0*Xs4V-IWhIkPyMkrn

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.

0*VtFzSr1hbSWDFFvk

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:

0*lMQ7ZO9SIbZemIsr

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.

0*1EUwBlZodwWMbX0v

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK