Submit the PHP component to packagist to publish your own composer package

Source: Internet
Author: User
Tags curl

Composer is a PHP-dependent management tool that gives PHP a new lease of life, a modern web development specification, packagist a library of PHP components, and other mirrors.

Submitted a PHP component of its own development on packagist so that other developers could use the composer to use the package. This component is not functional, mainly to look at the process of submitting PHP components, and recorded the process encountered problems and solutions for reference.


To submit a PHP component step:

1. Create a new project directory, creating a Composer.json file with the following format:

Php

{
"Name": "Your-vendor-name/package-name",
"description": "A short description of what your package does",
"Require": {
"PHP": "^5.3.3 | | ^7.0 ",
"Another-vendor/package": "1.*"
}
}


This JSON-formatted file contains the basic information about the component, which is also the way to load automatically, to specify the automatic loading mode according to the specific development pattern, where require can specify the other components that this component depends on, composer will automatically resolve dependencies.

Attach the complete Composer.json content as an example:

Javascript

{
"Name": "Tanteng/laravel-page-loaded-time",
"description": "Display Page Loaded Time",
"keywords": ["Laravel", "performance"],
"Homepage": "Https://github.com/tanteng/laravel-page-loaded-time",
"License": "MIT",
"Authors": [
{
"Name": "Tanteng",
"Email": "tanteng@qq.com",
"Homepage": "Http://www.tanteng.me",
"Role": "Developer"
}
],
"Require": {
"PHP": "^5.3.3 | | ^7.0 ",
"Laravel/framework": "~4.0"
},
"AutoLoad": {
"Psr-4": {"loading\\": ""}
}
}

2. Develop component features

Be careful to follow the PSR specification and use namespaces.

3. Submit the components to the GitHub

Before submitting the component to packagist, it is necessary to submit the code to the GitHub, where packagist only need to fill in the GitHub address of the component.

4. Submit component Address to Packagist

This completes the PHP component submitted to the Packagist process, please see Packagist official website specifically.

Problem: Component not found using composer require

The component was submitted to the Packagist, prompting the publication to succeed, but the component was not found using the composer command:

Php

Composer require Tanteng/laravel-page-loaded-time

As shown in figure:

Due to my composer use of the domestic mirror, speculation may be the reason for no synchronization, use this command to change the "source" back or not.

Php

Composer Config-g Repo.packagist composer https://packagist.org

It turns out that my component has not been officially published on GitHub, and this time it's a development version dev-master. Should add Dev-master version.

Php

Composer require Tanteng/laravel-page-loaded-time:dev-master

Sure enough, the Dev-master version is specified to successfully update the component, but this is not possible, we have to have an official version.

GitHub release version

Enter the component's GitHub home page, find the Navigation "releases", click into the picture page, you can create a version, fill in the information can be released after the version.

Follow the prompts on the page to fill out the content and publish it when you are finished. After the release, the package was found by composer require or unable to find it.

To set up an automatic update version

Originally also want to configure the automatic update on the GitHub. Specific steps reference: Https://packagist.org/about#how-to-update-packages

I set up the curl request directly by sending it manually, which is simpler, but this requires a request for a new version every time:

Php

Curl-xpost-h ' Content-type:application/json ' https://packagist.org/api/update-package?username=tanteng& Apitoken=secret '-d ' {"repository": {"url": "Https://github.com/tanteng/laravel-page-loaded-time"}} '

return {' status ': ' Success '} indicates success.

Open the Https://packagist.org/packages/tanteng/laravel-page-loaded-time again, the discovery is already the official version.

I used the composer domestic mirror, because the well-known reason, even the code warehouse to be the wall, I wear! Wait for a few hours to try again, this time the Mirror sync update, again input: Composer require tanteng/laravel-page-loaded-time, this time can be successfully updated. As shown, the vendor folder also automatically mounts dependent other components

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.