To access a project (like a library) on a private repository within your own Gitlab server, it is necessary to configure Composer right. First add the repository to the composer.json file of the current project (where you need the library code):
{
"repositories": [{
"type": "gitlab",
"url": "https://gitlab.yourserver.com/user/project.git" }]
The type “gitlab” activates some more configuration properties for Composer. So you have to define your private Gitlab server as valid server within Composer:
composer config --global gitlab-domains "gitlab.yourserver.com"
The –global flag stores the property within ~/.config/composer/config.json (OpenSuse 15.2). It will be activate for every Composer project on the computer.
If your repository is only visible after the login with a user/password, you must generate a unique token within the GitLab. Log into the GitLab UI with your username/password and choose “Preferences”->”Access Tokens”. There you can enter an application name like “PHP composer” and select “read_api” permissions. You get a new token, which you now can use within Composer config.
It is necessary to define the username and the access token for your Gitlab account, to provide access to the repository:
composer config gitlab-token.username "your GitLab username"
composer config gitlab-token.gitlab.yourserver.com "generated token"
Here you could also add –global or leave it and define the token only for the current project. The lines above should generate a file auth.json within your current project:
{
"gitlab-token": {
"username": "your GitLab username",
"gitlab.yourserver.com": "generated token"
}
}
Now you need a prepared repository on “https://gitlab.yourserver.com/user/project.git” (the library project). On the top level of the master branch there must be a composer.json file, which should not contain a version number, but a valid “name” property like (and “require” and “autoload” sections):
{
"name": "company/private_library"
}
The possible versions of your library will be extracted from the tags or branches of the project. Some Information about versions you can find on the composer website. The simplest way is to generate a tag of your latest commit and name it like “v1.0.0”. Now it is possible to define the library repository as a requirement within your current project:
{
"require": {
"company/private_library": "1.0.*"
}
}
You can use the version handling of Composer, if your tags will have valid version numbers. It is also possible to use stability constraints like “stable” or “dev”, use it within your tag name like “v1.0.0-dev”
{
"require": {
"company/private_library": "1.0.*@dev"
}
}
A following composer update should clone the repository into your vendor directory and the autoloader can provide the library project classes.