Home  >  Article  >  Development Tools  >  How to optimize vendor through composer in YII2

How to optimize vendor through composer in YII2

藏色散人
藏色散人forward
2019-08-12 14:16:402530browse

The following column composer usage tutorial will introduce how YII2 optimizes the vendor through composer. I hope it will be helpful to friends in need!

How to optimize vendor through composer in YII2

This article discusses the problems encountered in the process of installing the Yii2 framework and optimizing Vendor through the composer tool. It is agreed that readers have a certain understanding of the basic principles of composer and have practical experience in installing the Yii2 framework. experience.

We often encounter a type of problem in the Yii2 community, that is, after installing the officially recommended version

1 Why is there no vendor folder?

In Yii2, vendor is a dependent library file downloaded by composer. The official project template code only has its own project file, and the yii framework and other class libraries it depends on are all recorded in composer.json. Just install composer and then install or update.

2 In the Yii2 project that I installed myself, the packages in the vendor cannot be found in composer.json, and most of these packages are not needed for the time being. How can I remove them and keep the vendor minimized?

Verified scenario

2.1 Use composer remove to delete the package

The deleted package must be recorded in composer.json under the vendor folder, and be deleted after deleting the package At the same time, all packages will be reinstalled.

2.2 Use composer update --no-dev to delete the package

The package in development mode will be deleted directly. After deletion, run the project and an error will be reported directly. Therefore, all development mode packages cannot be deleted

How to optimize vendor through composer in YII2

How to optimize vendor through composer in YII2

2.3 Use remove codeception/verify to delete packages. Most of the packages in test mode are was deleted. According to my past development experience, components related to unit testing are not used, so you can use the remove codeception/verify command to streamline the Yii2 composer package. This command is a key part of optimizing the composer package.

3 When multiple projects are under one project, will there be loading performance problems due to too many composer packages being loaded for the first time? Can the built-in lazy loading solve this problem?

Components that are obviously unnecessary for the project can be removed directly. As for the performance impact, we have not encountered it yet. Lazy loading is considered a framework-level optimization, and I choose to believe in its ability.

4 Directory or file non-existence prompt caused by vendor path problem

Assume that our project path is E:sourcestemplates

When there is a problem with the Vendor path, the following similar problems will occur, that is, a certain composer package does not exist

Invalid Parameter – yiibaseInvalidParamException
The file or directory to be published does not exist: E:sourcestemplatesbackendvendorbower-asset/jquery/dist

The absence of the file package is just a symptom. The main reason is that the vendor path and the project configuration file are not correspond.

Under the main configuration file of the project, main.php, there is a configuration entry for the vendor. The specific value of VendorPath must be combined with the path of the configuration file. Please refer to the following two pictures and related path configurations

First picture

How to optimize vendor through composer in YII2

'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',

Second picture

How to optimize vendor through composer in YII2

'vendorPath' => dirname(dirname(dirname(__DIR__))) . '/vendor',

Summary:

We need to consider the following three issues when using compser

1 The difference between composer install and composer update

2 Composer version management and stability

3 Public How to output and maintain components in the form of composer packages

For the differences between the following commands, refer to the network information collection

composer install

should be the most commonly used command. Composer will download the package based on the local composer.json installation package. Put the package into the vendor directory under the project, and put the package version information during installation into composer.lock to lock the version.

In fact, during installation, if the composer.lock version is found to be the same as the current version If the code version in the vendor directory is consistent, Composer will do nothing. The purpose of composer.lock is to allow you to work with peace of mind under the current version without getting the latest version of the package.

composer update

composer The package version in the .lock file will be updated, and the packages recorded in composer.json will be updated to the latest.

The composer update command should be used with caution.

If the component has been referenced into the project using composer and becomes a public component, composer commands need to be used with caution. Of course, a dedicated person is required to be responsible for maintenance and does not have shared operation permissions.

For more composer usage tutorials, please visit the composer command usage graphic tutorial column!

The above is the detailed content of How to optimize vendor through composer in YII2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete
Previous article:How composer worksNext article:How composer works