The require file is a hassle, so use PSR-4 with Composer to load it once for life.

Feel similar to import in Java, oneself first record recently understood.

Manage your own packages in Composer

Install the composer


I won’t say much about this

English Tutorial

Chinese tutorial

PSR – 4 specification

PSR-4-autoloader

Build the project catalog

|-project

​ |-src

​ |-View.php

​ |-app

​ |-Tools.php

|-composer.json

The View in the path above is in project/ SRC/view.php. Otherwise, View and Tools are for demonstration purposes only.

Build composer. Json

Open the composer.json file you created and type it in. Take Monolog as an example.

{

“require”: {

"Monolog/monolog" : "1.2. *"

}}

Load the package with Composer

Type on the command line where the directory is located

Install Composer. Phar Install Composer. Install Composer for Windows by using the previous composer tutorial

Your directory should look like this after loading

|-project

​ |-src

​ |-View.php

​ |-app

​ |-Tools.php

​ |-vendor

| – composer / * composer directory the file is not in now

| – monolog / * in the same way

​ |-autoload.php

|-composer.json

|-composer.lock

Test the loaded Monolog package

Create an index.php in the project directory

#index.php <? php /** * Created by PhpStorm. * User: EasyChris<[email protected]> * Date: 2017/1/22 * Time: PHP file: require_once 'vendor/autoload.php'; $log = new MonologLogger('name'); $log = new MonologLogger('name'); $log->pushHandler(new MonologHandlerStreamHandler('app.log', MonologLogger::WARNING)); $log->addWarning('Foo'); echo 'success';

Address in the browser to access the file, usually should be http://127.0.0.1/project/index.php

Once you see the page Success, then check to see if there is an additional app.log file under the project folder. Congratulations on successfully loading the package in Composer and using the loaded package to log the output.

Load the file using PSR-4

1. Edit the composer.json configuration to load the directory and space name

Edit the composer.json file and add the autoload module.

{" require ": {" monolog/monolog" : "1.2. *}", "autoload" : {" PSR - 4 ": {" Acme" : "SRC /,"}}}

Where Acme:” SRC “means to bind the namespace Acme to /project/ SRC

Then modify the project/ SRC/view.php file

#View.php <? php /** * Created by PhpStorm. * User: EasyChris<[email protected]> * Date: 2017/1/22 * Time: 11:06 */ / namespace Acme; class View { public static function getView() { echo "get view"; }}

Go back to /project/index.php

#index.php <? php /** * Created by PhpStorm. * User: EasyChris<[email protected]> * Date: 2017/1/22 * Time: 10:50 */ require_once 'vendor/autoload.php'; use AcmeView; View::getView();

After modification, return to the command line to run

# using this command will automatically generate vagrant @ easychris autoload file: / var/WWW/public/test $composer dump - autoload Generating autoload files

PS: Need to pay attention!

Optimize: Composer dump-autoload --optimize: Composer dump-autoload --optimize: Composer dump-autoload --optimize

Finally open http://127.0.0.1/project/index.php if show the get the view, then, congratulations on your configuration was successful.

expand

And then you can go ahead and create something in the SRC directory like tools.php and you don’t have to do any other require to use it. Very convenient, you can try it by yourself.

conclusion

Check under the vender autoload. PHP file you will see that he is to return a ComposerAutoloaderInitaa62b39f628dda129dc5b4ebdfb2b2ae: : getLoader ();

#autoload.php <? php // autoload.php @generated by Composer require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInitaa62b39f628dda129dc5b4ebdfb2b2ae::getLoader();

And the getLoader() method does a few things

1. To find ComposerClassLoader if there is no is to generate an instance in the ComposerAutoloaderInitaa62b39f628dda129dc5b4ebdfb2b2ae for 2. All the autoload_psr4, autoload_classmap, autoload_namespaces(psr-0) generated by the Composer CLI are registered in the ComposerClassLoader. 3. Directly require all files in the AUTOLOAD_FILES