php - 使用正则表达式在 SilverStripe 中进行路由

标签 php regex silverstripe

在 SilverStripe 中我想创建动态路由。我试图路由的是与以下模式匹配的每个网址:一些数字和文本用破折号分隔,末尾为“-tx-i-numberid”

示例: http://example.com/some-text-and-numbers-separated-with-dashes-tx-i1654766

我读过这篇文章documentation我不明白。

我想要一些像下面这样的规则

'$Slug-tx-i$ID' : 'TeamController'

我想知道是否可以使用正则表达式或其他方式来做到这一点。

最佳答案

我不知道如何在路由系统中使用正则表达式来做到这一点。最好的选择是匹配“$Slug”,然后在 Controller 中解析它。

如果您的 Controller 操作实例化第二个 Controller 并传递请求,您实际上可以嵌套 Controller (请参阅 引用资料 以获得更深入的示例 https://vimeo.com/album/3629143/video/143149869 )。

例如:

class Controller1 extends Controller
{
    private static $allowed_actions = ['index'];
    public function index($req) {
        if (preg_match('/myregex/', $req->param('Slug'))) {
            return Controller2::create()->handleAction($request, $this->model);
        } else {
            return Controller3::create()->handleAction($request, $this->model);
        }
    }
}

关于php - 使用正则表达式在 SilverStripe 中进行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33789432/

相关文章:

php、curl、标题和内容类型

php - 将关联数组做成类似于 Eloquent 查询集合的集合

python - 如何从带有附加分隔符的 csv 在 python 中创建 pandas 数据框?

silverstripe - 如何在 silverstripe 中堆叠 CMS 字段标签?

javascript - 通过ajax获取分页页面的内容,过滤它并将其附加到当前页面

php - 无效的 SMTPAPI header - SendGrid

c# - 正则表达式从字符串中删除所有空格、句点和其他非单词字符

java - 正则表达式替换问题理解

image - 银条3 : How to get images from multiple pages for display on another page in a function?

带有特殊字符的 php,例如 ñ