Laravel Package Development Part 1 – Introduction

Laravel Package Development- Part 1 Introduction

Laravel Package Development ( 5 Lessons )

Maybe you’ve created a few apps with Laravel framework and now you want to create some re-usable code or a Laravel package. Where do you go to learn how to create a package? Yes, Right on this site!

see full series
  1. Laravel Package Development Part 1 – Introduction
  2. Laravel Package Development Part 2 – Adding Service Provider
  3. Laravel Package Development Part 3 – Adding Configuration File
  4. Laravel Package Development Part 4 – Session Class Constructor
  5. Laravel Package Development Part 5 – Shopping Cart CRUD

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.

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 :

packages/vendor/package/src

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.

cd packages/larashout/shopping-cart

And run below command:

git init

The above command will initialize an empty repository.

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

# 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.

git add .
git commit -m "initial commit"

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

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.

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.

composer init

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

{
    "name": "larashout/shopping-cart",
    "description": "A light weight shopping cart package for Laravel",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "LaraShout",
            "email": "[email protected]"
        }
    ],
    "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:

"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.

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.

Your little help will keep this site alive and help us to produce quality content for you.