Installation reference document: https://wizardforcel.gitbooks.io/slim3-doc/content/1.html
project directory
Among them, the main business operations are completed in the app directory, which can be divided into two directories according to the needs. Controller (controller directory) lib (class library directory), if necessary, you can add model (model layer)
The controller can be divided into Admin Home Common (public directory, which stores some public functions) according to the requirements.
entry file
Create index.php (entry file) and .htaccess (path rewriting) in the project public directory
In the entry file I instantiate the APP class and register the route
<? php // Import autoload require '../vendor/autoload.php' ; // Import configuration file $setting = require '../config/config.php' ; // Create an instance, pass in the configuration item $app = new \Slim\App( $setting ); //增 $app->post('/', '\Controller\home\GoodsController:addGoods'); //查 $app->get('/', '\Controller\home\GoodsController:showGoods'); //条件查询 $app->get('/{id}', '\Controller\home\GoodsController:searchGoods'); //改 $app->post('/modify', '\Controller\home\GoodsController:modifyGoods'); //删 $app->delete('/{id}', '\Controller\home\GoodsController:deleteGoods'); $app->run();
Registering a route needs to pass in two parameters, 1. route address 2. callback function
Callback functions support:
1. Ordinary functions
2. Anonymous functions
3. Class call
Here is the third way
If the callback function is called by the class name, you need to register the automatic loading first:
Add in composer.json in the root directory
{ "require": { "slim/slim": "^3.0", "monolog/monolog": "^1.23" }, "autoload": { "files": ["app/controller/common/function.php"], "psr-4":{ "Controller\\":"app/controller/", "Lib\\":"app/lib/" } } }
There is an autoload section in this config file for autoloading
It contains two main options: files and psr-4.
files is the function library (excluding classes) that needs to be automatically loaded by composer for us, as long as the file path of the function library is written in the following array.
psr-4, as the name suggests, is a class library based on psr-4 ( http://www.php-fig.org/psr/psr-4/ ) rules to automatically load the corresponding relationship, as long as the object after it starts with " Namespace": "Path" to write your own class library information.
After the modification is completed, composer install is required to take effect
Three parameters are supported in the callback function
Request/Request The first parameter is a Psr\Http\Message\ServerRequestInterface object representing the current HTTP request. Response/Response The second parameter is a Psr\Http\Message\ResponseInterface object representing the current HTTP response. Arguments array/Arguments The third parameter is an associative array containing named placeholders containing the current route.
When instantiating Slim\App, you can pass in a parameter, which can be a container instance or an array to configure the default container that is automatically created.
You can usually pass in an array of configuration items
<? php // Configuration file return array ( 'settings' => [ 'displayErrorDetails' => true , 'debug' => true , // Enable debug mode 'logger' => [ 'name' => 'slim-app', 'level' => Monolog\Logger::DEBUG, 'path' => __DIR__ . '/../logs/app.log', ] , // define the log file ], );