开发文档 v2.x

路由(Route)

更新时间:2024年3月5日 10:06 浏览:321

BE双驱框架通过路由识别用户访问的 应用.控制器.动作。

BE双驱框架的路由有以下三种形式:

标准的 GET 传输路由格式

  • https://www.phpbe.com/?route=Cms.Article.latest
  • https://www.phpbe.com/?route=Cms.Article.detail&id=xxxx

简单路由

  • https://www.phpbe.com/Cms/Article/latest
  • https://www.phpbe.com/Cms/Article/detail/id-xxx

高级路由(@BeRoute 注解实现)

  • https://www.phpbe.com/latest
  • https://www.phpbe.com/article-title

 

通过 系统 -> 参数 ->系统  中的 "是否开启伪静态" 控制

route-config.jpg

  • 不启用 - 即 标准的 GET 传输路由格式
  • 简单 - 即 简单路由
  • 路由器 - 即 高级路由(@BeRoute 注解实现)

 

其中,高级路由通过 @BeRoute 注解 实现,支持三种规则

  1. 静态路由
  2. 正则路由
  3. 哈希路由

具体规则参见 @BeRoute 注解

 

不管使用了哪种路由,在 Be 双驱框架中均需要使用公共函数 beUrl 生成网址

function beUrl($route = null, array $params = null)

参数:

  • string | null $route - 路径(应用名.控制器名.动作名)
  • array | null $params - 参数

返回值:

  • string - 生成的网址,根据配置不同,将返回不同格式的网址。

示例代码:

$url = beUrl('Cms.Article.latest');
// 伪静态 - 不启用:https://www.phpbe.com/?route=Cms.Article.latest
// 伪静态 - 简单:https://www.phpbe.com/Cms/Article/latest
// 伪静态 - 路由器:https://www.phpbe.com/article/latest

$url = beUrl('Cms.Article.detail', ['id' => 'xxx']);
// 伪静态 - 不启用:https://www.phpbe.com/?route=Cms.Article.detail&id=xxx
// 伪静态 - 简单:https://www.phpbe.com/Cms/Article/detail/id-xxx
// 伪静态 - 路由器:https://www.phpbe.com/article/article-title

$url = beUrl();
// 根网址:https://www.phpbe.com

 

访问使用了带参数的高级路由的网址时,参数将以 GET 方式还原

$url = beUrl('Cms.Article.detail', ['id' => 'xxx']);
// 伪静态 - 路由器:https://www.phpbe.com/article/article-title

访问上述网址:https://www.phpbe.com/article/article-title,GET 参数中将还原出 id 参数。

$id = Be::getRequest()->get('id');
// 将获取到 id 值 xxx

 

导航