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:

  1. 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).
  2. Once your finished with the project, you have to remove the PSR-4 autoload and the ServiceProvider 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 initand 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!

--

--

--

Completed my PHD in math. Since 2016 I work full-time on a family startup.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why WebAssembly is a Big Deal

Battle-tested template project for Kotlin Multiplatform

10 Full-stack Engineering Mistakes to Avoid: Still Make These Mistakes?

How to Hide your API keys in Python

How Snowflake empowers analysts to keep up with a changing world

Benefits Of Idempotent Design

Raydium and Magic Eden Join Forces to Boost Solana NFTs

Creating A Landscape Oriented Android Application

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Adam Nielsen

Dr. Adam Nielsen

Completed my PHD in math. Since 2016 I work full-time on a family startup.

More from Medium

How to generate custom error page in Laravel

Laravel Artisan How To Make Controller

Installing xdebug3 for PHP8 for mac users (PHPStorm + Laravel)

How to Setup Laravel Valet on Mac