Home > Article > Backend Development > A brief discussion of PHP components, frameworks and Composer
This article mainly introduces PHP components, frameworks and Composer, which has certain learning value. Interested friends can learn about it.
A component is a set of packaged codes, a series of related classes, interfaces and Traits that are used to help us solve a specific problem in PHP applications. For example, if your PHP application needs to send and receive HTTP requests, it can be implemented using ready-made components such as guzzle/guzzle. We use components not to reimplement functions that have already been implemented, but to spend more time achieving the long-term goals of the project.
Excellent PHP components have the following characteristics:
When we choose a framework, It takes a lot to invest in the tools of this framework. The framework usually provides a lot of tools, but when it does not provide a tool we need, the pain is passed on to us, and we have to find and integrate a custom PHP library. Integrating third-party code into a framework can be difficult because the third-party code and the framework may not use the same interfaces.
When choosing a framework, we focus on the future of the framework, but who can guarantee that a certain framework will always be the best tool to complete a certain job? Large projects that have been around for many years must perform well and make adjustments all the time. If you choose the wrong PHP framework, you may not be able to do this. Older PHP frameworks may be slow or outdated due to lack of community support. These old frameworks are often written using procedural code instead of modern object-oriented code and some of the new features of PHP. In short, when deciding whether to use a PHP framework, There are many things to consider.
Fortunately, Laravel has performed well in terms of these concerns, so it can stand out among many PHP frameworks. In a sense, Laravel is also a component-based development framework (the core component is its own Illuminate library , the function implementation relies heavily on third-party components). Compared with Symfony, it is easier to get started, so it has both scalability and ease of use. However, Laravel also has some shortcomings. For example, Laravel's own components cannot be easily decoupled and used outside the Laravel framework (but I believe this situation will improve, for example, its database and queue components can be decoupled). Taken together, Laravel is still an excellent framework that can help us quickly create powerful applications.
So should we use components or frameworks? The answer is, use the right tool for the right thing. If you can implement small projects quickly with some PHP components, use components. If you have multiple team members working on large projects, you can benefit from the agreed guidelines and structure provided by the framework. , then use a framework (if you are confused about which framework to use, then choose Laravel, it will not let you down). Using a framework can guide and accelerate the development of the project.
We look for PHP components in Packagist. This website is used to collect PHP components. The best PHP components can be found in Packagist.
For example, if we want to use an http component to send and receive HTTP messages, search http in the search box, and the first result we get is Guzzle, so use it.
Packagist is a community for finding PHP components, and Composer is a tool for installing PHP components. Composer is a dependency manager for PHP. It runs on the command line. You tell Composer which components you need, and Composer will download and automatically load these components into your project. It's that simple.
Composer and Packagist work closely. If you tell Composer that you want to use the guzzlehttp/guzzle
component, Composer will get the guzzlehttp/guzzle
component from Packagist and find this component. The warehouse address, determine which version to use, find out the dependencies of this component, and then download the guzzlehttp/guzzle
component and its dependencies into your project.
In addition, Composer will automatically generate autoloaders that comply with PSR standards for all PHP components in the project, effectively abstracting dependency management and automatic loading. Therefore, Composer is the most important to the PHP community. There is no such thing as additional tools. It is not an exaggeration to think about the painful days when we had to use include, require, and spl_autoload_register to manually implement automatic loading.
Regarding the installation and use of Composer, I will not go into details here. Please refer to the Composer Chinese website.
Below we use a sample project to demonstrate how to use Composer and components to develop a PHP application. The function of this application is to scan the URL in a CSV file to find dead links. The application An HTTP request will be sent to each URL. If the returned HTTP status code is greater than or equal to 400, the dead link will be sent to the standard output. This is a command line application. After development, we will execute this script, pass in the path of the csv file, and display the dead link list in the standard output.
Before we begin, let’s take a look at which tasks can be solved using existing PHP components: we need a component that can iteratively process the data of the csv file, and also add data to the csv file. Each URL sends an HTTP request, so you also need a component that can send HTTP requests and check the HTTP response.
After browsing Packagist, we found two components: guzzlehttp/guzzle
and league/csv
. The former is used to process HTTP messages, and the latter is used to process CSV data. Next, we run the following command at the top level of the project:
composer require guzzlehttp/guzzle composer require league/csv
Composer will install the dependencies to the vendor
directory in the root directory. After the installation is completed, composer will be generated in the root directory. .json
and composer.lock
files:
##composer.lock file will list all the files used by the project PHP components, as well as the specific version numbers of the components, actually lock the project so that the project can only use specific versions of PHP components. The advantage of this is that composer will download the specific version listed in this file, regardless of the latest version available in Packagist. You should put the
composer.lock file into version control so that team members can use it. The PHP version is the same as yours. If the PHP component versions used by local development and server are the same, bugs caused by different component versions can be minimized.
composer.lock, you can use the
composer update command.
scan.php file in the root directory, and then use
require# at the top of the file ##Import the autoloader created by Composer: <pre class="brush:php;toolbar:false;">require &#39;vendor/autoload.php&#39;;</pre>
The autoloader created by Composer is actually a file named
, which is saved in the vendor
directory When Composer downloads each PHP component, it will check the composer.json
file of each component to determine how to load the component. After obtaining this information, Composer will create a local PSR standard for the component. Autoloader. This way we can instantiate any PHP component in the project and these components are automatically loaded on demand. Writing code
code: <pre class="brush:php;toolbar:false;">//使用composer自动加载器
require &#39;vendor/autoload.php&#39;;
//实例Guzzle Http客户端
$client = new GuzzleHttp\Client();
//打开并迭代处理CSV
$csv = League\Csv\Reader::createFromPath($argv[1]);
foreach ($csv as $csvRow) {
try {
//发送HTTP GET请求
$httpResponse = $client->get($csvRow[0]);
//检查HTTP响应的状态码
if($httpResponse->getStatusCode() >= 400) {
throw new Exception();
}
} catch (Exception $e) {
//把死链发给标准输出
echo $csvRow[0] . PHP_EOL;
}
}</pre>
Below we use
Add some URLs, one per line, and at least one is a dead link:
Then open the terminal and execute the
scan.php script: <pre class="brush:php;toolbar:false;">php scan.php urls.csv</pre>
We passed in two parameters, the first is the path to the script file
, and the other is the path to the CSV file. The output is as follows:
Related tutorials:
PHP video tutorialThe above is the detailed content of A brief discussion of PHP components, frameworks and Composer. For more information, please follow other related articles on the PHP Chinese website!