How to Use Your Laravel Package Locally - Kevin McKee
Published on

How to Use Your Laravel Package Locally

Author

If you are building a Laravel package or maybe you've forked an open source package and want to make your own changes to it, you will most likely want to be able to use this package in a local project.

I am currently working on a Laravel package to auto-generate a bunch of scaffolding with a make:route command, so here's how I can get my package working in a local project so I can test it.

Create a New Laravel Project (Or Use An Existing One)

I'm going to create a new Laravel 6 project, but you can just as easily do this with one of your existing projects.

laravel new laravel6

This is my local project where I want to use the local package.

Update Your Project's composer.json File

The package I am working on is hosted on Github at intellow/make-route-for-laravel

The package itself is in the same directory as my new laravel6 project in a folder called "make-route-for-laravel", so the URL to access this code is "../make-route-for-laravel"

Now I open my composer.json file in this project and add the following to the end of the file. Don't forget to add a comma after the closing curly bracket of the "scripts" object if this is a brand new project.

"repositories": [
    {
        "type": "path",
        "url": "../make-route-for-laravel"
    }
]

Require the Package in Your Project

For this next step, it depends on if you've released your package on Packagist or not.

Your Package is in Development

If you are just starting on your package and it's not released yet on Packagist, then follow these instructions.

Once this is done I just go to my terminal in the laravel6 project and type

composer require intellow/make-route-for-laravel

Your Package is Released

If your package is released, then the above step will just get the same version anyone else can download.

You want to point to your local, so use the following command:

composer update intellow/make-route-for-laravel --prefer-source

You Are Ready for Local Package Development

Now your package is symlinked to your project. Now when I make updates to make-route-for-laravel, those updates will automatically be reflected in my laravel6 project. I don't have to do any composer updates or anything.

Happy package developing!

Want to talk about this post? Connect with me on Twitter →