To use your forked PHP package from GitHub in your project instead of the original package via Composer, follow these steps:
1. Update Your Fork's composer.json
Option A: Same Package Name (Replace Original)
Keep the original package name in your fork's composer.json to seamlessly replace it:
json Copy code
{
"name": "original/vendor",
...
}
Note: This may cause conflicts if other dependencies rely on the original package.
Option B: Unique Package Name (Recommended)
Change the package name to avoid conflicts (e.g., your-github-username/package):
json Copy code
{
"name": "your-github-username/package",
...
}
2. Modify Your Project's composer.json
Add your GitHub fork as a VCS repository and require it:
json Copy code
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/your-github-username/package"
}
],
"require": {
"your-github-username/package": "dev-master"
}
}
If using Option A (same name), replace your-github-username/package with the original package name (e.g., original/vendor).
dev-master ensures Composer fetches the latest commit from the master branch. Adjust the branch name if needed (e.g., dev-develop).
3. Install/Update Dependencies
Run:
bash Copy code
composer update your-github-username/package
This will pull the latest commit from your forked repository.
4. (Optional) Use a Specific Branch or Tag
If you want to track a different branch or tag:
json Copy code
"require": {
"your-github-username/package": "dev-your-branch"
}
Or for a tag:
json Copy code
"require": {
"your-github-username/package": "1.0.0"
}
5. Authentication (For Private Repos)
If your fork is private, configure GitHub authentication:
bash Copy code
composer config -g github-oauth.github.com <your-token>
Replace <your-token> with a GitHub Personal Access Token.
Example composer.json Snippet
json Copy code
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/your-github-username/forked-package"
}
],
"require": {
"your-github-username/forked-package": "dev-master"
}
}
Notes
Composer caches repositories. Use composer clear-cache if you encounter outdated versions.
Ensure your fork's branch (master by default) contains the desired changes.
This setup ensures your project always pulls the latest commit from your forked repository.