Tritech developer blogs

Tritech developer blogs

Add a package from GitHub repository in composer.json of your php project.

Photo by Luca Bravo on Unsplash

Add a package from GitHub repository in composer.json of your php project.

Check or test your package in your development environment before deploy production

Zia Uddin's photo
Zia Uddin
·Jun 7, 2022·

3 min read

About

Now a days we usually create php project by composer and add project depended package or module by composer. Today I am working a project using popular framework laravel. I need install a package through composer that conflict with my existing composer.json file.

its conflict with package dependency and found that package was last updated in 2 years ago and recently someone asking to update the dependency tree but for 2 months its not yet updated. So I plan to fork the repository and update the dependency tree and make a pull request but not sure when it accepted. So until accept my pull request I wish to use my fork repository as my package repository in my project composer.json So lets start

Process

Disclaimer: This is what I follow but this not only or standard process. But this is a quick successful process.

  • Get the github url from your git repository like :nzian/php-sslwireless-sms.git

github-repository-url.png

  • add the repository in composer.json as follows:
    "repositories": [{
          "type": "github",
          "url": "git@github.com:nzian/php-sslwireless-sms.git"
      }],
  • add a github token or generate a new token. Update you composer config as following command.
    php composer.phar config [--global] github-oauth.github.com token
    token replace with your github token.
  • When you generating the key you need to choose permission of your account. Please choose permissions carefully and keep the key safe. By this key anyone can perform the permission you gave before generate the key. I choose only public repository access if you want your private repository as well then choose repo permission as well. Here is my case and I choose 90 days for validity.

github-developer-token.png

permission-scope.png

  • Now you are ready to add your custom package from github repository. You can add any of the following command
    composer install
    or
    composer update
    or
    composer require nahid/ssl-wireless-sms
    
  • Sometimes its not updating any package or install or not found message in terminal like my case as follows:

github-package-error1.png Error1: I thought package name will be github username/repository name. But package name should be of the repository composer.json name key value. For example in my case it will be nahid/ssl-wireless-sms not nzian/ssl-wireless-sms.

github-repository-error2.png Error2: This is my lack of knowledge about composer package. I install lot of composer package but very few and publish may be one. My github username is Nzian so I try with Nzian/ssl-wireless-sms. But thats totally wrong because package name not related with github username or repository name its depened on composer.json file first key "name" value.

  • Another command you might be need to run if composer not found your repository
    composer clear-cache
  • After that I try to update with following command and it will install my package instead of default package.
    composer update "nahid/*"

github-repository-success.png

Summery

So process is not that complicated but If you don't know how to do it then its waste your valuable time. I write this process for my future reference but also help other developers to do it easily. Love open source.

#php #developer #composer #laravel #github #repository

 
Share this