Home > Backend Development > PHP Tutorial > PHPStorm+Composer quickly initializes project examples

PHPStorm+Composer quickly initializes project examples

小云云
Release: 2023-03-21 10:16:01
Original
6022 people have browsed it

PHPStorm is an excellent PHP IDE; loyal fans of vim can add an IdeaVim plug-in to continue using vim's functions. The combination of the two is a magical weapon. Of course, I also hope to be more advanced. The emergence of Composer makes the code organization and package dependencies of PHP projects simpler than before. What will happen if the three are combined?

Create project

Use PHPStorm to create a project, I won’t talk nonsense about this.
Whether you checkout a project through the version control system; or directly create a local project; or create a remote project... ...
I don't care. Just do it.

Create project organization structure

The so-called project organization structure, the most important thing is the directory structure. My personal habit is

project_root

    -webroot nginx或者apache或者iis的root目录        -js        -css        -img        -index.php  项目入口文件
    -vendor composer管理的包,库所在目录        -composer.json 
        -依赖的库,包等等,使用composer管理
    -apps 自己根据情况设定或者根据使用的框架来设定子目录
    -data 一些数据
    -docs 一些文档
Copy after login

It can be set freely according to personal circumstances, but There must be a vendor directory when using composer. You do not need to create the vendor directory manually. It will be created automatically when using composer.

Use composer to initialize the project

  • Download directly composer.phar package (phar package is similar to java jar package), placed in the project root directory
    When your working directory is workspace, the project directory is workspace/project_name, in fact, you can directly place composer.phar in the workspace directory Next
    In the workspace/project_name directory, execute php ../composer.phar command(command can be replaced with any available command)

  • Run PHPStorm's Terminal tool, execute composer related commands
    Of course you can also win+r keys, enter cmd, enter the dos command line, and then switch directories to the current project root directory to execute the command

  • php composer.phar init
    Initialize the composer.json file. When running the command, you will be asked to enter some information. Follow the prompts.
    Until Define your dependencies This step is to introduce external dependencies

  • php composer.phar install
    Install the dependency package to the vendor directory. If there is no vendor directory, it will be created automatically

Of course you can also Manually create composer.json in the project root directory, and then php composer.phpar install
If you want to delete the dependency on a certain package, you can only manually delete the package in the vendor directory, and thenphp composer.phar update

By the way, when creating a new project in phpstorm, you can choose to create a composer project, so why use the command line? The reason is that the composer library has been loading.
I don’t know what gfw did or how. Although the command line is slower, it will definitely succeed.
Fuck GFW.

Composer Common Commands

composer list  列出所有可用的命令
composer init   初始化composer.json文件(就不劳我们自己费力创建啦),会要求输入一些信息来描述我们当前的项目,还会要求输入依赖包
composer install  读取composer.json内容,解析依赖关系,安装依赖包到vendor目录下
composer update   更新最新的依赖关系到compsoer.lock文件,解析最新的依赖关系并且写入composer.lock文件
composer search packagename 搜索包,packagename替换为你想查找的包名称
composer require packagename 添加对packagename的依赖,packagename可修改为你想要的包名称
composer show packagename
composer self-update 更新 composer.phar文件自身

composer command --help 以上所有命令都可以添加 --help选项查看帮助信息
Copy after login

For more detailed and accurate command information, please visit composer cli command

The above is the detailed content of PHPStorm+Composer quickly initializes project examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template