Install local packages on your laravel project

Erland Muchasaj
4 min readMar 17, 2023

--

Local package loader

Lately, I started to create some packages for Laravel and PHP. And of course as always, when developing, you should be able to create and test your packages locally.
Also when you want to make a DRY approach to some of your code and prevent yourself from repeating the same code in multiple projects, you can extract it into a package and load it locally.

They are also a great way to make your code sharable and reusable.

Composer offers a useful way to install and load local packages.

First, create a directory on your root project folder. I prefer packages/vendor-name/package-name .

For the laravel packages by convention is better to use laravel prefix.
For example:

Let’s create the first package

Create a folder called laravel-hello inside packages/vendor-name/laravel-hello on the root folder of a laravel project.
The folder structure will look something like this:

package development folder structure

The vendor-name usually, should correspond with your GitHub username.
The src folder is where all the magic is.

Now we go inside the laravel-hello package by running the following:

cd ./packages/erlandmuchasaj/laravel-hello

Then we initialize the composer by running the following command:

composer init

This command will prompt you with some questions regarding the package being created but for now, you can accept the default one. You can always edit them later on composer.json .

Here is an example of how the composer.json file would look like.

initializing composer.json

After the initialization we can go to packages/erlandmuchasaj/laravel-hello/composer.json and edit it as follows:

{
"name": "erlandmuchasaj/laravel-hello",
"description": "A simple Laravel package showing rock paper scissors lizard spock rules..",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Erland Muchasaj",
"email": "erland.muchaasaj@gmail.com"
}
],
"autoload": {
"psr-4": {
"ErlandMuchasaj\\LaravelHello\\": "src/"
}
},
"require": {
"php": "^8.1"
},
"minimum-stability": "dev",
"prefer-stable": false
}

The last two attributes are not required, but they could help us if we run into any errors getting our local package to load in composer.

Pay close attention to autoload object.

This composer.json file specifies that the package has a namespace ErlandMuchasaj\LaravelHelloand that any classes in that namespace should be loaded from the src/ directory. It also specifies that the package requires PHP version 8.1 or higher.

Inside the /src folder we create a PHP class called RockPaperScissorsLizardSpock.php.

<?php

namespace ErlandMuchasaj\LaravelHello;

class RockPaperScissorsLizardSpock
{

public function rules(): string
{
return 'Scissors cuts paper,
Paper covers rock,
Rock crushes lizard,
Lizard poisons Spock,
Spock smashes scissors,
Scissors decapitates lizard,
Lizard eats paper,
Paper disproves Spock,
Spock vaporizes rock,
and as it always has, rock crushes scissors.';
}

}

Keep in mind that the package composer.json is different from project composer.json.

Now open the root directory composer.json file and add repositories section. This section should include a reference to your local package directory.

{
...
"repositories": [
{
"type": "path",
"url": "./packages/erlandmuchasaj/laravel-hello",
"options": {
"symlink": true
}
}
]
}

Passing in the "symlink": true option means that our package's source folder will be symlinked into the vendor directory of our PHP project and we don't need to make composer update each time we make an update to the package.

After that, we just need t update our require list with our newly created package.

    "require": {
"php": "^8.1",
"erlandmuchasaj/laravel-hello": "@dev", // <== load package
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.0",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
},

Notice as the package name that we used in the require list corresponds with the name of the package in the composer.json above.

Now all you need to do is run composer update and voilà.

Testing if it works

You can now use your local package in your project just like any other package like this:

<?php

use ErlandMuchasaj\LaravelHello\RockPaperScissorsLizardSpock;

$game= new RockPaperScissorsLizardSpock();

echo $game->rules();
Scissors cuts paper,
paper covers rock,
rock crushes lizard,
lizard poisons Spock,
Spock smashes scissors,
scissors decapitates lizard,
lizard eats paper,
paper disproves Spock,
Spock vaporizes rock,
and as it always has, rock crushes scissors.

After you have finished developing your package locally, you can remove it from repositories section of your project composer.json file and publish it to a package repository like Packagist so others can also use it.

Publishing the package in Packagist is not in the scope of this article but you can read more about it here.

Feel free to Subscribe for more content like this 🔔, clap 👏🏻 , comment 💬 and share the article with anyone you’d like

And as it always has been, I appreciate your support, and thanks for reading.

--

--

Erland Muchasaj
Erland Muchasaj

No responses yet