Learn how to create symbolic links for your local packages, set up Composer to automatically read these packages, and require them in your Laravel project. This guide offers step-by-step instructions and best practices for Laravel package management, ideal for developers aiming to streamline their workflow and increase productivity.
Here is your step-by-step guide:
Create a new directory for your package
Create a new directory for your package inside a
packages/vendor/my-package
directory in the root of your Laravel application. For instance, if your package is namedmy-package
and your vendor name ismy-vendor
, you could create a directory like so:packages/my-vendor/my-package
.Develop your package
Inside the
my-package
directory, you can start developing your package. The structure of a Laravel package can vary, but it usually includes asrc
directory, acomposer.json
file, and various other files and directories as needed (e.g.,routes
,views
,migrations
, etc.).
You can use this excellent Laravel package boilderplate from BeyondCode here: https://laravelpackageboilerplate.com/
Update your root composer.json file
In your Laravel application's root
composer.json
file, you need to add a path repository pointing to your local package. This tells Composer where to find your package. Here's an example of what that might look like:"repositories": [ { "type": "path", "url": "./packages/vendor/my-package", "options": { "symlink": true } } ]
In this example,
"./packages/vendor/my-package"
is the relative path from the root of your Laravel application to the directory containing your package.Require your package
Next, in the
"require"
array of yourcomposer.json
file, you'll need to include your package, like so:
"require": {
"/vendor/my-package": "*"
}
In this example,
"/vendor/my-package"
is the name of your package as defined in thecomposer.json
file of your package.
Install your package
Then, you'll need to include your package. Run
composer update
from the root of your Laravel application. This will install your package into thevendor
directory, creating a symbolic link to your local package directory.Use your package
Now you can start using your package within your Laravel application. Note that since you're using a symbolic link, any changes you make to the package in the
packages/my-vendor/my-package
directory will be reflected in thevendor
directory.
Remember to follow the standards and best practices for developing Laravel packages, such as using service providers to register your package's resources (routes, views, migrations, etc.) and publishing assets and configuration files.
Bonus
If you are developing multiple packages and you want composer to symlink all the packages inside of the packages
directory, you can include a wildcard /packages/*
value to your composer.json file, like so:
"repositories": {
"type": "path",
"url": "./packages/*",
"options": {
"symlink": true
}
}
Working with local Laravel packages doesn't have to be a complex task. By creating symbolic links for your packages and setting up Composer to read them, you can streamline your development workflow significantly. Whether you're working on your own packages or collaborating with other developers, these strategies will help you manage your local packages with greater efficiency and ease.