Composer Local Package Development
Composer is a great tool for dependency management in PHP. We can build/create any php library or package by using composer. In this blog I will discuss how to setup local environment php package development using composer.
Set Local Repo Path
When we install any package in our application, we can see the package folder/directory in the vendor folder. If we develop any package locally, the development files or local package name is required in composer JSON file, but the composer installs the libraries or packages normally from the remote dir Packagist. So we have to setup like below to tell the Composer to load our package from the local repository or folder. To do this, add this code to your composer JSON file
<code class="language-javascript"> "repositories": [ { "type": "path", "url": "path to your local package dir" } ] </code>
For example
<code class="language-javascript"> "repositories": [ { "type": "path", "url": "codeboxr/writelog" } ] </code>
In my case, codeboxr is the folder where store my all-local packages and where writelog is my package name.
Set Local Package as Required
In the second step, we have to tell the composer file that our package is ready to require for local development. So we will need to specify that as require
<code class="language-javascript"> "require": { "codeboxr/writelog": "dev-main" } </code>
If your main project is not in the git main branch when while developing package locally you have to specify the branch name. For example, I am developing my package when my main project is in the dev branch then specify the branch name in the required package.
<code class="language-javascript"> "require": { "codeboxr/writelog": "dev-dev" } </code>
Installing Local Package
In the final stage, we have to install our new local developed package by using this command.
<code class="language-javascript"> Composer install </code>
After running this command we can see our writelog package is available in the vendor folder which looks like a symbolic link.