SlimPhp uses routing management page access, Slim itself does not have MVC pattern, but we can implement MVC effect through Controller, since our goal is to write interfaces, so for now we don’t worry about View layer, just dealing with MC. It is recommended to see Akrabat’s simple Slim project to understand Slim’s run logic Glihub address :Slim-Bookshelf

File structure

The recommended file directories are as follows: # indicates the level-1 directory, and ## indicates the level-2 directory

#app ---- Main directory of the project
## helloWordController.php ---- Controller for test
##dependencies. PHP ---- Add the controller file you want to use
##routes.php ---- define the route
##setting.php ---- configures the configuration file
#public ---- Directory that users can access
# # index. PHP -- -- the index page
#vendor ---- automatically generated directory using Composer
Json ----composer configuration file
##composer. Lock ----composerCopy the code

Create a directory

Create file directories app and public according to the directory structure above. Vendor directory is automatically generated using composer command, so we do not need to create.

Install the Slim

The Slim installation can be seen in an article.

Edit the composer. Json

Edit the composer. Json file, add the Autoload address, and then execute the terminal to perform the Composer Update

{" require ": {" slim/slim" : "^ 3.0}", "autoload" : {" PSR - 4 ": {" Sample \ \" : "app" / / "Sample" is defined in a namespace, "app" is the corresponding file path}}}Copy the code

Edit the Controller file

Create the helloWordController.php file in the app folder and post the HelloWordController code

namespace Sample\Controllers; // Namespace, very important, reference files need to use this path

use \interop\Container\ContainerInterface;

final class HelloWordController
{
    protected $app;

    public function __construct(ContainerInterface $ci)
    {
        $this->app = $ci;
    }
    /** * test method **/
    public function say($request, $response, $params)
    {
        echo 'Welcome Slim'; }}Copy the code

Edit the index.php file

Create an index. PHP file in the public folder to handle network access

inpublicCreate an index. PHP file inside the folder to handle network accessuse \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '.. /vendor/autoload.php'; // Auto-loaded scripts

$settings = require '.. /app/setting.php'; // Import the configuration file for the Settings
$app = new \Slim\App($settings);

require '.. /app/dependencies.php'; // Import the controller configuration file
require '.. /app/routes.php'; // Import the routing management file

$app->run(); / / executionCopy the code

Write the Settings configuration file

Create setting.php in the app directory and write the configuration file according to your needs

return [
    'settings'= > ['addContentLengthHeader'= >false.'displayErrorDetails'= >true // Enable error message]];Copy the code

Write the Controller configuration file

Create dependencies. PHP in the app folder. Note that all files need to use the namespace path, as in “Sample\Controllers\HelloWordController”.

$container = $app->getContainer();

//hello
$container['Sample\Controllers\HelloWordController'] = function ($c) {
    return new \Sample\Controllers\HelloWordController($c);
};Copy the code

Add a route management file

Create the routes.php file in the app folder

// Handle domain name /hello address access, jump to$app->get('/hello'.'Sample\Controllers\HelloWordController:say');Copy the code

Finished?

So now when we go to the web site, does that give us what we expect? But that’s not true. Controller not fund. What bug is this? Composer dump-Autoload -o, composer dump-autoload -o, composer dump-autoload -o, composer dump-autoload -o