赞
踩
由于Hyperf只能运行在Linux和Mac上 所以要用到PHPstrom的ftp上传及下载
1.Hyperf的传统路由定义在app\config\routes.php下的,配置方式和laravel是一样的,这个就不过多的讲解了
2.Hyperf通过注解的方式定义路由(首先我们需要下载phpstrom的IDE注解插件PHP Annotations),在PHPstrom设置的Plugins 中搜索 PHP Annotations,下载重启PHPstrom
@AutoController() 注解会自动根据类名及方法名创建对应的URL
- <?php
-
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://doc.hyperf.io
- * @contact group@hyperf.io
- * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
- */
- namespace App\Controller;
- use Hyperf\HttpServer\Annotation\AutoController;
-
- /**
- * @AutoController(prefix="user")
- * prefix参数会重定义类名 使Url自定义
- */
- class IndexController extends AbstractController
- {
- public function index()
- {
- $user = $this->request->input('user', 'Hyperf');
- $method = $this->request->getMethod();
-
- return [
- 'method' => $method,
- 'message' => "Hello {$user}.",
- ];
- }
- }
如果没有加prefix参数的话 url是http://localhost:9501/index/index
如果定义了prefix参数 url就是 http://localhost:9501/user/index
如果类名是IndexDiController这样驼峰式并且没有配prefix参数的话 url是http://localhost:9501/index_di/index
@Controller() 注解
@Controller()注解需要搭配 @RequestMapping() @GetMapping() @PutMapping() @PostMapping()等注解来一起使用
- <?php
-
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://doc.hyperf.io
- * @contact group@hyperf.io
- * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
- */
- namespace App\Controller;
- use Hyperf\HttpServer\Annotation\AutoController;
- use Hyperf\HttpServer\Annotation\Controller;
- use Hyperf\HttpServer\Annotation\RequestMapping;
-
- /**
- * @Controller(prefix="index")
- */
- class IndexController extends AbstractController
- {
- /**
- * @RequestMapping(path="index", methods={"get","post"})
- * #path规定了路由里对应该方法的名称,methods则规定了访问的方式
- * 注意参数要带引号而且必须是双引号
- */
- public function index()
- {
- $user = $this->request->input('user', 'Hyperf');
- $method = $this->request->getMethod();
-
- return [
- 'method' => $method,
- 'message' => "Hello {$user}.",
- ];
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。