Table of Contents
准备工作
新建一个目录(示例)
初始化 composer.json文件
校验composer.json
添加自动加载 (composer.json)
在Composer.php编写逻辑
写完直接提交git
最后将git地址放到https://packagist.org 官网
提交新本版到仓库 会自动同步到 https://packagist.org
测试
Home Development Tools composer How to write Composer package

How to write Composer package

May 17, 2021 pm 04:57 PM

下面由composer教程栏目给大家介绍Composer包的编写,希望对需要的朋友有所帮助!

准备工作

1,有自己的github账号
2,有自己的https://packagist.org 网站的账号
Copy after login

新建一个目录(示例)

laravel
|--src
|  |-composer.php
Copy after login

初始化 composer.json文件

在laravel文件下
 composer init   命令

// 1. 输入项目命名空间
// 注意<vendor>/<name> 必须要符合 [a-z0-9_.-]+/[a-z0-9_.-]+
Package name (<vendor>/<name>) [dell/htdocs]: output/test=
// 2. 项目描述
Description []: 这是一个测试
// 3. 输入作者信息,可以直接回车
Author [, n to skip]:  sinker <2713497141@qq.com>
// 4. 输入最低稳定版本,stable, RC, beta, alpha, dev
Minimum Stability []: dev
// 5. 输入项目类型,
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
// 6. 输入授权类型
License []: MIT
// 7. 输入依赖信息
Would you like to define your dependencies (require) interactively [yes]?
// 如果需要依赖,则输入要安装的依赖
Search for a package: php
// 输入版本号
Enter the version constraint to require (or leave blank to use the latest version): >=5.4.0
// 如需多个,则重复以上两个步骤
// 8. 是否需要require-dev,
Would you like to define your dev dependencies (require-dev) interactively [yes]?

// 操作同上
{
    "name": "output/test",
    "description": "composer",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "sinker",
            "email": "2713497141@qq.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}
// 9. 是否生成composer.json
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]?
Copy after login

校验composer.json

composer validate composer.json
Copy after login

添加自动加载 (composer.json)

"autoload": {
        "psr-4": {
            "Composer\\": "src/"
        }
    }
Copy after login

在Composer.php编写逻辑

namespace Composer;   ## 注意点此处要和自动加载出相同

class Test
{
    public function save()
    {
        echo &#39;composer-test&#39;;
    }
}
Copy after login

写完直接提交git

git tag 1.0.0  
git push --tag    打版本号
Copy after login

最后将git地址放到https://packagist.org 官网

How to write Composer package
How to write Composer package
How to write Composer package

提交新本版到仓库 会自动同步到 https://packagist.org

测试

How to write Composer package

The above is the detailed content of How to write Composer package. 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)

What is a composer used for? What is a composer used for? Apr 06, 2025 am 12:02 AM

Composer is a dependency management tool for PHP. The core steps of using Composer include: 1) Declare dependencies in composer.json, such as "stripe/stripe-php":"^7.0"; 2) Run composerinstall to download and configure dependencies; 3) Manage versions and autoloads through composer.lock and autoload.php. Composer simplifies dependency management and improves project efficiency and maintainability.

What is a composer doing? What is a composer doing? Apr 08, 2025 am 12:19 AM

Composer is a dependency management tool for PHP, used to declare, download and manage project dependencies. 1) Declare dependencies through composer.json file, 2) Install dependencies using composerinstall command, 3) parse the dependency tree and download it from Packagist, 4) generate the autoload.php file to simplify automatic loading, 5) optimize use includes using composerupdate--prefer-dist and adjusting the autoload configuration.

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:

Composer Expertise: What Makes Someone Skilled Composer Expertise: What Makes Someone Skilled Apr 11, 2025 pm 12:41 PM

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

What is the difference between composer and orchestrator? What is the difference between composer and orchestrator? Apr 02, 2025 pm 02:49 PM

Composer is used to manage dependencies on PHP projects, while Orchestrator is used to manage and coordinate microservices or containerized applications. 1.Composer declares and manages dependencies of PHP projects through composer.json file. 2. Orchestrator manages the deployment and extension of services through configuration files (such as Kubernetes' YAML files), ensuring high availability and load balancing.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

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.

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.

See all articles