Caleb Porzio

Bash Alias: composer-link - Require Local Folders as Composer Dependencies

Apr 2018

Anytime I find myself contributing to or building a composer package, I inevitably feel the need to test it in a real Laravel project.

I've written before about solving this problem before using composers "repositories" feature. The problem is, every time I go to link up a local repository, I forget what you have to do and pull up that blog post.

Inspired by npm link (which is awesome btw), I created my own little composer-link bash function to make this operation happen in one quick step.

Usage

Let's assume the following folder structure:

- sites
  - laravel-project
  - composer-package

From within the laravel-project folder, run the following:

composer-link ../composer-package

Now you can just require the package into the project as you normally would.

composer require [vendor]/composer-package

([vendor] is the vendor name you specify in the package's composer.json file)

Note: if the composer require doesn't work the first time, you may need to add @dev to the command:

composer require [vendor]/composer-package @dev

The Source

I wrote composer-link in a simple bash function. All you have to do to start using it is add it to your: ~/.bashrc, ~/.bash_profile, or ~/.zshrc:

composer-link() {
    composer config repositories.local '{"type": "path", "url": "'$1'"}' --file composer.json
}

Like anytime you change one of these files, you have to "source" it to start using it. Run: source ~/.bashrc to make the new bash function available to your terminal.

Hope you found this helpful. If you use it and like it, be sure to spread the love on twitter.


My Newsletter

I send out an email every so often about cool stuff I'm working on or launching. If you dig, go ahead and sign up!