Create Your Own Laravel Package in 10 Minutes

Published on May 27, 2020 1 min read

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": [

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.


namespace Mynotepaper\Greeter;

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

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:

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


Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Follow Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *