Basic introduction to using Composer

藏色散人
Release: 2020-04-05 17:16:49
forward
3054 people have browsed it

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

Basic introduction to using Composer

Composer 的使用

1. 下载composer.phar文件

在项目根目录下,使用cmd或bash运行下列命令:(需安装PHP)

php -r "readfile('https://getcomposer.org/installer');" | php
Copy after login

2. 初始化Composer

使用cmd或bash运行下列命令,输入相应信息后,完成了composer的初始化:

composer init
Copy after login

此时根目录下多了一个名为composer.json的配置文件。

3. 将自己写的类文件加入composer的自动加载

假设一部分类文件在文件夹./app/db/下,并定义了命名空间db。则应在composer.json文件中的第一层花括号中添加如下项:

"autoload": { "psr-4": { "db\\": "app/db/" } }
Copy after login

整个composer.json文件类似这样:

{ "name": "linnzh/front", "type": "project", "license": "MIT", "authors": [ { "name": "Linnzh", "email": "reg.lynnzh@gmail.com" } ], "description": "关于前端的一些尝试", "require": {}, "require-dev": {}, "autoload": { "psr-4": { "db\\": "db/" } } }
Copy after login

该键值对表示composer加载器以psr-4的方式加载组件,并告诉加载器,app/db/目录下的类文件将映射到命名空间db。

4. 更新配置

修改了composer.json文件后,必须运行以下命令来生成vendor/autoload.php文件:

composer install
Copy after login

此时将在项目根目录下生成一个vender文件夹,并且其中包含一个autoload.php自动加载类文件,使用已定义且在composer.json中配置的类文件时,可直接引入该文件,而不必使用多条require语句。

更多composer技术文章,请访问composer栏目,地址是://m.sbmmt.com/tool/composer/

The above is the detailed content of Basic introduction to using Composer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!