我的 routes.yml
中有一个自定义路由,它将任何未知请求转发到 BaseController
'$URLSegment/$Name/$Action/$ID': 'BaseController'
从那里,请求被路由(以及其他地方)到我的主 Controller ,由 Controller 的 index()
处理。
但是它总是使用 index()
所以如果我转到 URL test1/test2/action/5
它仍然会由 index 运行()
这是我的BaseController
class BaseController extends ModelAsController {
public function getNestedController() {
$action
$params = $this->getRequest()->params();
$this->loadMain($params['URLSegment'], $params['Name'], $params['Action'], $params['ID']);
}
private function loadMain($first, $name, $action, $id) {
$main = new MainController();
$main->{$action}();
}
}
这将调用该函数,但是 index()
函数已经运行并设置模板。
我知道我可以从 index()
调用该函数并返回这样的模板,但是我相当确定这会绕过我热衷于维护的允许操作的安全功能.
我已经在我的 MainController
中定义了 $allowed_actions
并添加了:
private static $url_handlers = array(
'$URLSegment/$Name/something/$ID' => 'something'
);
但它仍然只是调用index()
。
如何通过自定义路由 MainController
维护 SilverStripe 的相同操作路由
最佳答案
您还需要定义private static $allowed_actions
,也许还需要定义private static $url_handlers
,后者提供自定义路由。
基本上所有的东西都来自:https://docs.silverstripe.org/en/3/developer_guides/controllers/routing/
关于php - 在 ModelAsController 子类上调用操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41154931/