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
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.
<?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:
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.
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)