首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hyperf 初体验-路由

Hyperf 初体验-路由

作者头像
hedeqiang
发布2019-12-18 11:30:45
1.1K0
发布2019-12-18 11:30:45
举报
文章被收录于专栏:LaravelCodeLaravelCode

这次我们来了解下 Hyperf 的路由。主要包括以下内容

  • 如何定义路由
  • 路由的寻址规则
路由定义

路由主要有两种方式来定义

  • 配置文件定义路由
  • 通过注解来定义路由
配置文件定义

config/routes.php 文件中定义所有路由

主要有以下几种方式

<?php
use Hyperf\HttpServer\Router\Router;

//传入闭包
Router::get('/hello-hyperf', function () {
    return 'Hello Hyperf.';
});

// 下面三种方式的任意一种都可以达到同样的效果
Router::get('/hello-hyperf', 'App\Controller\IndexController::hello');
Router::get('/hello-hyperf', 'App\Controller\IndexController@hello');
Router::get('/hello-hyperf', [App\Controller\IndexController::class, 'hello']);

同时 Hyperf 也提供了一些常用的路由

use Hyperf\HttpServer\Router\Router;

// 注册与方法名一致的 HTTP METHOD 的路由
Router::get($uri, $callback);
Router::post($uri, $callback);
Router::put($uri, $callback);
Router::patch($uri, $callback);
Router::delete($uri, $callback);
Router::head($uri, $callback);

// 注册任意 HTTP METHOD 的路由
Router::addRoute($httpMethod, $uri, $callback);
通过注解来定义路由

注解来定义路由主要有以下两种方式

  • @AutoController 注解
  • @Controller 注解

使用 @AutoController 注解时需 use Hyperf\HttpServer\Annotation\AutoController; 命名空间;

@AutoController 注解
<?php
declare(strict_types=1);

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;
    }
}
@Controller 注解
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Controller\Controller as  AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @Controller()
 */
class IndexController extends AbstractController
{
    /**
     * @RequestMapping(path="index", methods="get,post")
     */
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}

将会生成 index/index 路由,http://127.0.0.1:9501/indx/index

同时 Hyperf 提供了 5 种 注解,分别是:

使用 @GetMapping 注解时需 use Hyperf\HttpServer\Annotation\GetMapping; 命名空间; 使用 @PostMapping 注解时需 use Hyperf\HttpServer\Annotation\PostMapping; 命名空间; 使用 @PutMapping 注解时需 use Hyperf\HttpServer\Annotation\PutMapping; 命名空间; 使用 @PatchMapping 注解时需 use Hyperf\HttpServer\Annotation\PatchMapping; 命名空间; 使用 @DeleteMapping 注解时需 use Hyperf\HttpServer\Annotation\DeleteMapping; 命名空间;

使用方式如下:

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Controller\Controller as  AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @Controller()
 */
class IndexController extends AbstractController
{
    /**
     * @GetMapping(path="index")
     * @PostMapping(path="index")
     */
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}
路由寻址

Hyperf 的注解依靠 命名空间来实现 URL。例如:namespace App\Controller\User;,则路由就是 /user/**

注解参数

@Controller@AutoController 都提供了 prefixserver 两个参数。

prefix 表示该 Controller 下的所有方法路由的前缀,默认为类名的小写,如 UserControllerprefix 默认为 user,如类内某一方法的 pathindex,则最终路由为 /user/index。 需要注意的是 prefix 并非一直有效,当类内的方法的 path/ 开头时,则表明路径从 URI 头部开始定义,也就意味着会忽略 prefix 的值。

server 表示该路由是定义在哪个 Server 之上的,由于 Hyperf 支持同时启动多个 Server,也就意味着有可能会同时存在多个 HTTP Server,则在定义路由是可以通过 server 参数来进行区分这个路由是为了哪个 Server 定义的,默认为 http

更多使用方式 请查看 官方文档 路由

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 路由定义
  • 配置文件定义
  • 通过注解来定义路由
    • @AutoController 注解
    • @Controller 注解
    • 路由寻址
    • 注解参数
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档