在 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/