Origin
Maple has been a few years before the development of PHP, most of the open source framework under the guidance of writing code. Now still, instinct will let me use a PHP framework to develop PHP applications, but also because lazy bar, do not have a good study in addition to the framework of some things.
Today and everyone talked about, but also later met a lot of PHP framework, found that there are many framework tools do not support the function, have to write or go outside to find. To make the most of the wheels as little as possible, Maple brings a more practical tool for you to quickly and easily find the features you want and integrate them into your PHP application.
It is a component.
Component definition
Components are packaged code to help you solve a specific problem in your PHP application. classes, interfaces, and traits in a component are typically placed in the same namespace.
Component components are very simple, don't expect a component to solve a lot of problems for you, the components must be functionally single.
The component may be a PHP file, a class, very simple.
Using the right tools to do the right thing, if you have a chance, or want to build a small project that accurately solves the problem with some PHP components, the components help to keep the code lightweight and flexible.
Select components
We can find the PHP component in https://packagist.org/.
If you are interested in what components of PHP are good, you might want to go to this link.
https://github.com/ziadoz/awesome-php
There are a lot of good PHP components listed in this link.
If you want an HTTP request-related component, enter HTTP in the search box above, and you will see a list of components related to the HTTP request.
I suggest that according to Word of mouth to choose the above components, if you feel too much trouble, according to the number of star bar, or more reference.
Using PHP components
Using a PHP component must address two issues, dependency management, and automatic loading. Of course, we also have the tools to solve it.
Composer is a tool for installing PHP components, and composer is also a dependency manager for PHP components, running on the command line.
Composer can work with packagist, if you need to download components through composer, composer will get the relevant components via packagist.
Composer's role is important, depending on the management and automatic loading will make you very headache, because PSR-4, the dependency manager composer will automatically generate a PSR-compliant autoloader for all PHP components in the project. Composer solves the challenges of dependency management and automatic loading.
How to install composer
Everyone can install according to the official documents: https://getcomposer.org/
My side is to provide a Mac OS and Linux installation method, SSH to the remote machine, start a pleasant installation.
$curl-ss Https://getcomposer.org/installer | PHP$MV Composer.phar/usr/local/bin/composer
In case of permission problems, please sudo yourself. Let's enter the composer command at the command line to see the effect.
#composer
The following screen shows that you have successfully installed the composer.
If your composer is in disrepair, it will remind you to upgrade. Simply enter the following command to complete the upgrade.
$composer self-update
Using composer
The name of the component is typically the Enterprise name/package name. For example, we just searched for PHP in packagist to return the list, guzzle/http in the guzzle is the enterprise name, HTTP is the package name. The enterprise name is globally unique, which is the global identifier used to identify who owns the package. The package name is used to uniquely identify a package in the enterprise name.
Packagist will list all versions of the component (including the dev version being developed), but we do not need to go to a version of a version of the filter, composer will help us do this.
How do I download this HTTP request component? At this point, we'll start with a CD in the command line to the top-level directory of the project where we want to download the component, and enter the following command to download the Guzzle/http component.
#composer require guzzle/http
This command will let composer find and install the latest stable version of the specified PHP component. In this way, you can have an HTTP request related to the PHP component, is not very simple.
The following prompt indicates that the component we are downloading has already been downloaded successfully!
For the yellow part of the hint, we do not care about him, this is just a component test, if you need to use the full set, the following command is recommended.
#composer require Guzzle/guzzle
When you execute this command, you will create two files in the top-level directory of your project: Composer.json and Composer.lock, and remember that all two files need to be included in the version control system.
This component will eventually be placed in the vender/directory of your project's top level directory.
Composer.json
This file must be a valid JSON file, as to whether it is valid, you can copy the above code, to this site to authenticate:
http://www.bejson.com/
It will tell you that your file is not a standard JSON.
Composer uses the information in this file to find, install, and automatically load the PHP components.
Full format of Composer.json file see composer official website: https://getcomposer.org/
Composer.lock
This file will list all the PHP components used by the project, and the specific version number of the component, which is actually similar to the file lock, process lock, and so on.
Why do you need to include this file in a version control system, because you need to let your other team members know what versions of the PHP components The project uses, to avoid the risk of defects due to component version differences.
Working with Components
PHP components downloaded, how to use it, for example, we downloaded the HTTP component under the Vender directory, we need to add the following sentence in our project's entry file (typically index.php)
Require ' vendor/autoload.php ';
Composer when downloading a PHP component, it also creates a PSR-compliant autoloader for all dependencies on the project. We just need to add this code to our project portal file. This allows us to instantiate any PHP component in the project, which is automatically loaded on demand.
Using the methods and functions inside the component, you typically use the following code:
$loop = React\eventloop\factory::create (), $socket = new React\socket\server (8080, $loop); $http = new React\http\server ( $socket) $http->on (' request ', function (Request $request, Response $response) { $response->writehead (200, Array (' content-type ' = ' text/plain ')); $response->end ("Hello world!\n");}); $loop->run ();
So, it's done. Today and the people said here, as to how to use the components inside, in the packagist have a detailed explanation of the DA, we can spend more time, more research and research components, you will have a great help. ^_^
"PHP Series" PHP components in a detailed