During the module development process, developers typically incorporate the module into the app/code/ directory. Have you ever contemplated the method of installing the module via Composer and testing its behavior on different instances? It is indeed feasible to install your local module via Composer without the necessity of uploading it to Packagist or any external platform. Uncover the benefits of harnessing Composer to establish a local repository and vendor in Magento. By meticulously following these steps, you can effortlessly integrate and proficiently manage your custom modules.
- Begin by creating a designated directory to store your local code. I recommend using a folder named “.packages” within the Magento base directory. However, you have the freedom to choose a different name according to your preference.
- Establish a vendor name, such as “webficial,” for your separate vendor.
- Create a new module or copy an existing module from a local setup, following the module composer standards. Refer to the example path below.
# Step 1
$ mkdir .packages
# Step 2
$ cd .packages
$ mkdir vendor
# Instead of step 1,2 use Step 3
mkdir -p .packages/webficial/
- 4. Add the local-src subdirectories as a Composer path repository. This can be done manually or through the following command:
Manually updating the project’s composer.json file:
"repositories": {
"webficial": {
"type": "path",
"url": ".packages/webficial/*",
"options": {
"symlink": true
}
},
"0": {
"type": "composer",
"url": "https://repo.magento.com/"
}
}
- Using the command line :
composer config repositories.vendor_name path '.packages/vendor/*'
# Example:
composer config repositories.webficial path '.packages/webficial/*'
- Install the module using Composer.
$ composer require module-composer-name
# Example:
composer require webficial/module-core
or
composer require webficial/module-core 1.0.0
Conclusion:
When creating a composer repository using the command, the symlink option is automatically included in the repositories list of the current repo path option in the root composer.json file. This ensures that Composer considers the symlink value as true.
By using the command to create the composer repository and subsequently installing the module through Composer, a symbolic link module will be generated under the vendor/namespace path. This link points to the local package stored in the .packages/vendorname/module-composer-name directory.
If you prefer to move the code into the vendor directory instead of using symlinks, you can update the symlink value from true to false or add the appropriate options. I have already set the symlink option value when manually adding the repository path in the root composer.json file.
In conclusion, learning to install a local module via Composer is essential for Magento developers. This approach, seamlessly integrating custom modules, ensures standardized practices and easy deployment. By adopting Composer for local module installation, you streamline workflows and fortify your Magento development process. Embrace Composer for efficiency and elevate your Magento projects.