Table of Contents
前言
确定厂商名和包名
确定命名空间
生成composer.json文件
实现组件功能
版本控制
提交到packagist
使用这个组件
Home Development Tools composer How to use composer to create PHP components

How to use composer to create PHP components

Apr 19, 2021 pm 05:16 PM
composer php

下面由composer教程栏目带大家介绍怎么使用composer创建PHP组件,希望对需要的朋友有所帮助!

How to use composer to create PHP components

使用composer创建PHP组件

前言

当今php生态环境,composer已经成为必不可少的组件,相信大多数人已经使用过别人的composer包, 那么如何创建一个PHP组件,发布到packigist从而把你的创意贡献给开源社区呢?

  1. 确定厂商名和包名

    在开发PHP组件之前,首先要选择组件的厂商名和包名,已用来区分组件属于谁,建议厂商名和包名都是用小写字母.

  2. 确定命名空间

    命名空间不必和包名一直,注意不要与其他组件命名空间重复.

  3. 生成composer.json文件

    我们可以简单使用composer init 创建项目的composer.json 文件,
     之后会以问答的形式提示你填写信息, 以下 ">" 后 表示交互时输入的信息

    > composer init
    
                                                
      Welcome to the Composer config generator  
                                                
    
    
    This command will guide you through creating your composer.json config.
    
    Package name (<vendor>/<name>) [composertest/example]:  # 厂商名与包名
    > suntianxiang/cn-oauth
    
    Description []: > A library for wechat,alipay,weibo OAuth2 Client # 详细信息
    
    Author [stx , n to skip]:
    
    Minimum Stability []: > beta # 下载时倾向开发版还是稳定版
    
    Package Type (e.g. library, project, metapackage, composer-plugin) []: 
    > library # 包的类型,我们属于library,固填library
    
    License []: MIT # 开源协议
    
    Would you like to define your dependencies (require) interactively [yes]? > no # 是否需要定义你的依赖
    
    Would you like to define your dev dependencies (require-dev) interactively [yes]?  > no
    
    
    {
        "name": "suntianxiang/cn-oauth",
        "description": "A library for wechat,alipay,weibo OAuth2 Client",
        "type": "library",
        "require-dev": {
            "phpunit/phpunit": "^6.5"
        },
        "license": "MIT",
        "authors": [
            {
                "name": "stx",
                "email": "334965556@qq.com"
            }
        ],
        "minimum-stability": "beta",
        "require": {
            "php": "> 7.0",
            "guzzlehttp/guzzle": "^6.3"
        },
        "autoload": {
            "psr-4": {
                "CnOAuth\\": "src"
            }
        }
    }</name></vendor>
    Copy after login

    最后会输出生成的composer.json文件 ()

  4. 实现组件功能

    现在我们要实现组件的具体功能了。这一步我们要便携组成PHP组件的类、接口和形状。编写什么类以及编写多少类完全取决于PHP组件的作用。
    组件的文件组织方式如下:

      Root:
          src/
              这个目录包含组件的源码 (例如PHP文件)
          tests/
              单元测试文件
          composer.json
              composer配置文件
          README.md
              组件介绍,说明,使用方法等, markdown格式
          ...
    Copy after login

    如果你还不太清楚,可以参考一些优秀的组件仓库的组织结构

  5. 版本控制

    在把组件发布到packagist之前,我们还需要把组件发布到公开的代码仓库中,
    例如 github, 把写好的代码发布到了github上 并创建一个release

How to use composer to create PHP components

  1. 提交到packagist

    现在可以把组件提交到packagist了。
    登录packagist后, 单机网页右上角的 “Submit Package”按钮,然后在 “Repository URL” 中输入完整的Git仓库URL,再单机“Check”按钮。组件创建完成

How to use composer to create PHP components

  1. 我们可以在Github上创建一个钩子,每次更新组件的仓库时通知Packgist,在仓库中设置钩子的方法参阅https://packagist.org/about#how-to-update-packages

  2. 使用这个组件

    一切都完工了!现在任何人都能使用Comopser安装这个组件了。

     composer require suntianxiang/cn-oauth
    Copy after login

The above is the detailed content of How to use composer to create PHP components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

See all articles