Create Your Own Laravel Package in 10 Minutes

In this article, I’m going to create a simple Laravel package. I’m testing in Laravel 7.13. So, let’s get started:

Table of Contents

  1. Create Laravel Project
  2. File Structure
  3. Initialize composer.json
  4. Create a Class
  5. Testing Our Package

Create Laravel Project

Run this command to create a Laravel project:

laravel new project-name
# or
composer create-project — prefer-dist laravel/laravel project-name

File Structure

Now let’s create a file structure as shown below to develop our package.

We’ve created packages folder on the default folder structure of a new Laravel project. Inside the packages folder, we’ve defined mynotepaper as the vendor name and greeter as the package name. The src folder will contain all the source code of our package.

Initialize composer.json

Go to packages/mynotepaper/greeter directory from command prompt. Then run this command to initialize composer.json:

composer init

This command will ask for some questions. After the initialization, we need to add a property named autoload like below:

{
    "name": "mynotepaper/greeter",
    "description": "A simple Laravel package",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Md Obydullah",
            "email": "[email protected]"
        }
    ],
    "autoload": {
        "psr-4": {
            "Mynotepaper\\Greeter\\": "src/"
        }
    },
    "minimum-stability": "dev",
    "require": {}
}

Open Laravel project’s composer.json file & add "Mynotepaper\Greeter\": "packages/mynotepaper/greeter/src" in autoload>psr-4 property :

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Mynotepaper\\Greeter\\": "packages/mynotepaper/greeter/src"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ]
},

Now we need to run the following command in the project root directory for package discovery:

composer dump-autoload

Create a Class

Navigate to packages>mynotepaper>greeter>src folder. Inside src folder create a class file named Greet.php.

Greet.php
<?php

namespace Mynotepaper\Greeter;

class Greet
{
    public function greet(String $name)
    {
        return 'Hello ' . $name . '! Welcome to Mynotepaper.com';
    }
}

This class will greet user. As this is a short package tutorial, I’m not going to add more classes or functions. Let’s try to access this class from Laravel project.

Testing Our Package

Let’s create a route and call our package Greeter class from this route:

routes/web.php
use Mynotepaper\Greeter\Greet;

Route::get('/greet/{name}', function ($name) {
    $greet = new Greet();

    return $greet->greet($name);
});

Now run the Laravel project and visit http://localhost:8000/greet/Obydul URL to see the output.

We’ve created a simple Laravel project and tested. You can download this project from GitHub. Thank you.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.