How to create a Laravel Package inside a Laravel application
Laravel has an excellent documentation how to create a package. Also Lasse’s instructions and DevDojo’s instructions have become popular.
In Lasses and DevDojos instructions it is explained how one can setup a package inside a new Laravel application. It’s of course a lot simpler to develop a package if you can test it in a live Laravel app. I would like to compare their approaches, and show another approach, that I find very convenient.
DevDojos Approach
DevDojo creates the package inside a new Laravel app. He then adds PSR-4 autoload
manually in the composer.json and registers the package ServiceProvider
manually in the Laravel app.
This approach is got if you just develop a new package inside a dummy Laravel application. However, it is bad if you want to develop the package in an existing git-tracked Laravel application, because:
- You cannot use git on your Laravel application and for the package folder separately (You could use git submodules, but then there is no compatibility with composer).
- Once your finished with the project, you have to remove the
PSR-4 autoload
and theServiceProvider
manually, to fetch the package with composer.
Lasse’s Approach
Lasse creates his package in a new folder. He mentions at the end of the article that one may just require it by composer, if one wishes to include it in a Laravel app. The benefit of this in comparison to DevDojos approach is that one can skip to manually add PSR-4 autoload
and the manual binding of the ServiceProvider
. Also, one is entitled to use git for the package, aswell as the Laravel application containing it.
This will symlink the package folder in the vendor directory of your Laravel app in your composer.json
:
"repositories": [
{
"type": "path",
"url": "../mypackage",
"options": {
"symlink": true
}
}
],
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"myvendor/mypackage": "dev-master"
}
Another Approach
I like to start my package as a GitHub repository. I clone the new repository and initialize composer first composer init
and push it back to the repository.
Now I create a fresh Laravel application and add the GitHub url from my repository to the composer.json
:
"repositories":[
{
"type": "vcs",
"url": "git@github.com:yourvendor/yourpackage.git"
}
],
We should also make sure to call composer dump-autoload
after we have changed the json file.
Next, I require it in my fresh Laravel app:
composer require yourvendor/yourpackage --prefer-source
The --prefer-source
option will track the package folder automatically with git. So when I do changes in the downloaded folder, I can just push them to my repository. Of course, its a bit cumbersome to cd into the vendor/yourvendor/yourpackage
folder. To avoid that one may create a symlink. In Ubuntu this can be done by
ln -s vendor/yourvendor/yourpackage/ packages/
Remember that symbolic links are relative to the location where the link is in. You also have to create the folder packages
before hand.
That’s it. I love this setup. Hope you too!