DIY implements thinkephp core framework Composer

What is Composer?

Composer is a tool used by PHP to manage dependencies. It can automatically install these dependent library files. In addition, composer can automatically load classes and create its own packages. Installation of Composer Very simple.

Implement automatic loading of classes

Create composer.com in / home/web/mytp directory JSON file

{
  "autoload": {
      // Build namespace app imitating ThinkPHP
    "psr-4": {"app\\": "application/"}
  }
}

The above code indicates that the automatically loaded class file must comply with the PSR-4 specification, app \ \ indicates the namespace app, and application / indicates the application directory, which means mapping the namespace app to the application directory. When you need to load the class of namespace app, look for the corresponding file in the application directory. The sub namespace of namespace app will be mapped to the corresponding directory under the application directory.

         Namespace                                 catalogue
app\index\controller\Index <=> application/index/controller/Index.php

Enter composer JSON directory, execute the composer install command to initialize automatic loading, and install the components required by the corresponding dependencies. Output under normal conditions

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

After the operation is completed, a vendor directory will be generated under the current directory, with the following structure

vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── autoload_static.php
        ├── ClassLoader.php
        ├── installed.json
        └── LICENSE

When you need to update dependent packages or library files, you only need to modify composer The JSON file to the required version, and then execute the composer update command. After the namespace of the introduced mechanism is changed, the name of the template does not need to be displayed

EmployeeController.php => Employee.php
EmployeeModel.php => Employee.php
// The class name in the corresponding file also needs to be changed to comply with the PSR-4 specification

In the entry file index Introducing vendor/autoload.php php

<?php
require '../vendor/autoload.php';

Edit Shell script start SH realizes the startup and self start of Nginx and PHP FPM

#!/bin/bash
#chkconfig:2345 80 30
#description:auto_run
#
#

/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/php-fpm
systemctl stop firewalld

Add script to boot auto start project

chkconfig --add start.sh

Visit DIY tp/index. php

The automatic loading function of class has been realized

Tags: PHP thinkphp composer

Posted by tripleM on Thu, 19 May 2022 20:23:47 +0300