Laravel

Laravel Package Development Part 1 - Introduction

In this series Laravel Package Development, we will look at how we can create Laravel Packages and use them.

2 April 2019Updated 23 April 20265 min read923 words
Laravel Package Development- Part 1 Introduction
Last updated:

In this series Laravel Package Development, we will look at how we can create Laravel Packages and use them.

We will be creating a lightweight shopping cart package which can be used in an e-commerce application.

You probably already have a good grip and making some cool applications using the Laravel framework. Now you are wondering, how you can make some of the application code reusable which can be thrown in other applications.

Laravel provides a simple ecosystem which is extendable and packages play a major role in moving it forward. Official packages by Laravel and other open source contributor have made our lives and code better.

Why We Should Use Laravel Packages?

First of all, we all must understand why we should create packages and are important. There are many reasons to justify Laravel Package development, here I am listing some of them:

  • Better Skills: By publishing Laravel packages, you are updating your skill. As soon as you put your code in the public, you will get the feedback from developers around the world which will help you to improve and learn writing better code.
  • Save Time: Writing a package means, you write code which can be reused and integrated into other applications. You will be saving yourself a considerable amount of time when you convert a piece of code, that you are using in more than one project.
  • Build Brand: By publishing Laravel packages, you will be building your brand, as developers and clients see and use your code, you will get more recognition.
  • Contributing to Open Source Community: By building packages, you are giving back to Open Source community. Just imagine, how many packages you use in your application and how much time you save using packages. Maybe you could help other developers and could save the time of others.

Creating First Laravel Package

Now, let's dive into creating a new package in Laravel. We will start by creating a new Laravel application, run below command in terminal to generate a new Laravel project.

bash
composer create-project laravel/laravel Package

Next, we will need to create a new folder called packages in the root of the application, where we will create all our packages. The name packages is really up to you, you can call it anything.

Laravel Package Folder Structure
Laravel Package Folder Structure

We will place all our package files in this folder. Now create couple of folders inside this folder following the basic structure for any PSR-4 package, like below :

css
<strong>packages/vendor/package/src</strong>
Laravel Package Folder Structure
Laravel Package Folder Structure

For example, larashout is our vendor name, and shopping-cart is our package name. If you are hosting the package on Github then vendor name should be your Github Username. src is the folder where we will add all our code.

Setting Up Git

Once we have the folders created, it's the best time to initialize a git repository, so we can push our package to a code hosting repositories like Github, Bitbucket or Gitlab. I will be using the Github and I have created a shopping-cart repository to host this package.

Now go to shopping-cart folder and initialize a empty git repository.

bash
cd packages/larashout/shopping-cart

And run below command:

code
git init

The above command will initialize an empty repository.

Add a new README.md file in the shopping-cart folder with below content:

php
# LaraShout Shopping Cart

Also add a .gitkeep file in the src folder.

Once you have created the files, run below commands to add the file to git and commit it.

sql
git add .
git commit -m "initial commit"

Now we have to add a remote origin to our repository.

bash
git remote add origin https://github.com/LaraShout/shopping-cart.git

Caution

Please change the url in above code to your repository one.

Once you have added the remote origin, run below command to push it to the repository.

code
git push origin master

Composer Setup

To load this package using Composer, we will initialize the composer in the same shopping-cart folder. Run below command to initialize composer.

code
composer init

Answer the questions and it will creat a composer.json file with below content.

json
{
    "name": "larashout/shopping-cart",
    "description": "A light weight shopping cart package for Laravel",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "LaraShout",
            "email": "contact@larashout.com"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}

Choose any license you like, for minimum stability I recommend to use stable. Once you have a composer.json file generated, add it to git and make a new commit to keep track of your composer.json file.

Namespacing and Autoloading

To automatically load your package in Laravel, go to composer.json file in your application route and look for autoload section. In "pasr-4" insert the location of your package like below:

json
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "LaraShout\\ShoppingCart\\": "packages/larashout/shopping-cart/src"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
]
},

What we’re doing is we’re autoloading the package ourselves this way we can test it offline and not have to run composer update every time something new comes up. Since we have updated our composer.json file, we will need to regenerate our autoload files.

code
composer dump-autoload

If you don't get any error while dumping autoload, you are on the right track.

In the next post, we will start creating the real package from this point and look at how we can create service providers and facades in our package.

If you have any question or comment, please leave them in the comment box below.