Swoole frame Hyperf (III) - and Route Controller

When we accessed via ip + port the browser when visiting a controllerapp/Controller/IndexController.php/index

It is config/routes.phpconfigured
Here Insert Picture Description
now, we try to create a new access controller!
However, the traditional routing configuration would not say it, one can understand.
So, we use annotations to configure routing.
What annotations are?
You understood as a method of routing configuration inside the controller.

@AutoController annotations , generated automatically routed
in app/Controller/a newUserController.php

<?php
declare(strict_types=1); // php严格模式

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface; // 接收请求
use Hyperf\HttpServer\Annotation\AutoController; // 自动路由

/**
 * 下面的注释不能去掉,它是有作用的,也就是注解
 * @AutoController()
 */
class UserController
{
    // Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
    public function index(RequestInterface $request)
    {
        // 从请求中获得 id 参数
        $id = $request->input('id', 1);
        return (string)$id;
    }
}

Restart hyperf service
Here Insert Picture Description
Here Insert Picture Description
@Controller comment , configure the routing
or in UserController.phpyears, covering the code

<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @Controller()
 */
class UserController
{
    // Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
    /**
     * @RequestMapping(path="index", methods="get,post")
     */
    public function index(RequestInterface $request)
    {
        // 从请求中获得 id 参数
        $id = $request->input('id', 1);
        return (string)$id;
    }
}

Remember when modified the code to restart the service Oh!
Here Insert Picture Description
necessary

/**
 * @Controller()
 */
/**
  * @RequestMapping(path="index", methods="get,post")
  */

@Controller()This indicates that the controller
@RequestMappingis configured item, we can modify path, to indexchange index2, and then access the
restart service
you will find, indexnot visit, while index2you can access.
You can even change this

/**
  * @RequestMapping(path="/abc/index2", methods="get,post")
  */

Restart the service
Here Insert Picture Description
path to /at the beginning, from the root directory represents the beginning of what you want to configure anything.

methodsRequest type is allowed.

Hyperf frame Series List

Published 112 original articles · won praise 75 · views 130 000 +

Guess you like

Origin blog.csdn.net/weikaixxxxxx/article/details/95750608