When we accessed via ip + port the browser when visiting a controllerapp/Controller/IndexController.php/index
It is config/routes.php
configured
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
@Controller comment , configure the routing
or in UserController.php
years, 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!
necessary
/**
* @Controller()
*/
/**
* @RequestMapping(path="index", methods="get,post")
*/
@Controller()
This indicates that the controller
@RequestMapping
is configured item, we can modify path
, to index
change index2
, and then access the
restart service
you will find, index
not visit, while index2
you can access.
You can even change this
/**
* @RequestMapping(path="/abc/index2", methods="get,post")
*/
Restart the service
path
to /
at the beginning, from the root directory represents the beginning of what you want to configure anything.
methods
Request type is allowed.