开发文档 v2.x

@BeRoute 注解

更新时间:2024年2月20日 21:54 浏览:256

@BeRoute 注解用于控制器类的方法上,添加此注解后,该方法对应的页面使用注解中配置的参数作为访问路径。

@BeRoute 注解 支持三种规则

 

静态路由

静态路由的参数为固定的字符串,适用于该方法不需要传递参数或参数不需要产生伪静态效果的场景:

<?php

namespace Be\App\Cms\Controller;

/**
 * 文章
 */
class Article
{

    /**
     * 最新文章
     *
     * @BeRoute("/article/latest")
     */
    public function latest()
    {
        // ...
    }

    // ...

}

以上代码示例中,将可以通过网址:https://www.phpbe.com/article/latest 访问该页面

 如果静态路由对应的方法需要传参数,该参数将作为普通GET参数传递:

$url = beUrl('Cms.Article.latest');
// 将编译出网址:https://www.phpbe.com/article/latest

$url = beUrl('Cms.Article.latest', ['category_id' => 'xxx']);
// 将编译出网址:https://www.phpbe.com/article/latest?category_id=xxx

 

正则路由

  • 单个参数 @BeRoute("/xxx/<(正则表达式):参数名>");
  • 多个参数 @BeRoute("/xxx/<(正则表达式):参数名>/<(正则表达式):参数名>/...");

 

代码示例:

<?php
class Order
{

    /**
     * 订单明细
     *
     * @BeRoute("/order-<(\d+):order_id>.html");
     */
    public function detail() {
        $orderId = Be::getRequest()->get('order_id');
        // ...
    }

}

每组 尖括号(< >) 里的内容作为一个匹配组,冒号(:)前为正则匹配语法, 冒号(:)后为 GET参数名。

$url = beUrl('ShopFai.Order.detail', ['order_id' => 'xxx']);
// 将编译出网址:https://www.phpbe.com/order-xxx.html

 

哈希路由

俣希路由通过调用方法返回可变路由值

<?php

namespace Be\App\Cms\Controller;

/**
 * 文章
 */
class Article
{

    /**
     * 文章明细
     *
     * @BeRoute("\Be\Be::getService('App.Cms.Article')->getArticleUrl($params)")
     */
    public function detail()
    {
        $id = Be::getRequest()->get('id', '');
        // ...
    }

}

@BeRoute("\Be\Be::getService('App.Cms.Article')->getArticleUrl($params)");

路由的值表示调用了一个服务类的方法,返回一个唯一的字符串作为访问网址。其中的 $params 取网址参数。即 beUrl 编译网址时的 GET 参数。

beUrl('Cms.Article.detail', ['id' => 'xxx']);

 

Service App.Cms.Article 中的 getArticleUrl 方法定义

<?php

namespace Be\App\Cms\Service;

class Article
{

    // ...

    /**
     * 获取文章伪静态页网址
     *
     * @param array $params
     * @return string
     * @throws ServiceException
     */
    public function getArticleUrl(array $params = []): string
    {
        $article = $this->getArticle($params['id']);
        return '/article/' . $article->url;
        // return '/article/' . $article->url  . '.html’;
        // return  $article->url  . '.html’;
    }

    // ...


}

示例中使用了数据库的一个唯一字段 url 作为网址(该字段跟据 文章标题 自动生成(如:汉字转成拼音),也可手工输入)

方法的返回值没有固定的格式要求,只要唯一即可。

$url = beUrl('Cms.Article.detail', ['id' => 'xxx']);
// https://www.phpbe.com/article/xxx
// https://www.phpbe.com/article/xxx.html
// https://www.phpbe.com/xxx.html

 

效率

静态路由 > 哈希路由 > 正则路由

 

静态路由:

静态路由可直接定位到方法上,另外它的基数也不大,所以它的效率最高

 

哈希路由:

Be 双驱框架的 哈希路由 底层由一个双向的 哈希索引实现, 使用 Redis 存储,效率很高,可以放心使用。

以 beUrl('Cms.Article.detail', ['id' => 'xxx']) 为例,假设解析后网址为:/article/vvv.html

首先对网址中的路由 + 参数 进行哈希操作(md5),得到唯一值 hash-key

在 Redis 中将存入两条唯一索引记录. 

  • 索引A 键名 : hash-key,健值:/article/vvv.html
  • 索引B 键名: /article/vvv.html,健值:json_encode(['Cms.Article.detail', ['id' => 'xxx']])

索引A 用于 beUrl 编译网址

索引B 用于 访问该网址时解析出原始数据

 

正则路由:

正则路由是大部分框架主流实现方案,数量较多的会影响网站性能,尽量避免使用。

 

 

导航