Return Home

Loading Composer Packages Locally

Introduction

Working with Composer in PHP projects is a standard practice for managing dependencies. However, when developing a custom Composer package, you might want to test and refine it within the context of a real project without publishing it to a package repository like Packagist. This guide will walk you through loading a Composer package from a local directory, such as packages/vendor/package-name, ensuring a seamless development process.

Why Choose a Local Directory?

Choosing a local directory for package development has its perks:

Step-by-Step Guide

1. Organize Your Directory Structure

Ensure your project has the following structure:

your-project/
├── composer.json
└── packages/
    └── vendor/
        └── package-name/
            ├── src/
            └── composer.json

2. Update Your composer.json

Modify the composer.json file in your main project to include the local package:

{
    "repositories": [
        {
            "type": "path",
            "url": "packages/vendor/package-name"
        }
    ],
    "require": {
        "vendor/package-name": "*"
    }
}

3. Run Composer Update

Execute the following command in your main project's root:

composer update

Composer will then symlink (or copy) the local package into the vendor directory of your main project.

Best Practices for Local Package Development

While developing with local packages is convenient, remember:

Conclusion

Developing Composer packages locally is an efficient way to streamline your workflow. By following the steps outlined above, you can test and refine your packages within the context of a real project, without the hassle of publishing to a repository. Remember to transition to a more formal package management approach as your package stabilizes and prepares for production use.

Happy coding! 🚀

Written © 2024 Written Developer Tutorials and Posts.

𝕏