我正在设置到 Controller 的路由,但我不断收到 404 或“开始使用 silverstripe 框架”页面。
在 routes.yaml 我有:
---
Name: nzoaroutes
After: framework/routes#coreroutes
---
Director:
rules:
'view-meetings/$Action/$type': 'ViewMeeting_Controller'
我的 Controller 看起来像这样:
class ViewMeeting_Controller extends Controller {
public static $allowed_actions = array('HospitalMeetings');
public static $url_handlers = array(
'view-meetings/$Action/$ID' => 'HospitalMeetings'
);
public function init() {
parent::init();
if(!Member::currentUser()) {
return $this->httpError(403);
}
}
/* View a list of Hospital meetings of a specified type for this user */
public function HospitalMeetings(SS_HTTPRequest $request) {
print_r($arguments, 1);
}
}
我创建了一个模板 (ViewMeeting.ss),它只输出 $Content,但是当我刷新站点缓存并访问/view-meetings/HospitalMeetings/6?flush=1
我得到默认的“开始使用 Silverstripe 框架”页面
我知道 routes.yaml 中的路由正在工作,因为如果我更改那里的路由并访问旧 URL,我会收到 404,但请求似乎不会触发我的 $Action...
最佳答案
您的 YAML 和 Controller 中有 2 个不同的规则($type 与 $ID)。另外,我认为您不需要在 YAML 和 Controller 中定义路由。
试试这个,YAML 告诉 SS 将所有以 'view-meetings' 开头的内容发送到您的 Controller ,然后 $url_handlers
告诉 Controller 根据 'view' 之后的所有内容如何处理请求-URL 中的“ session ”。
路由.yaml
---
Name: nzoaroutes
After: framework/routes#coreroutes
---
Director:
rules:
'view-meetings': 'ViewMeeting_Controller'
ViewMeeting_Controller.php
class ViewMeeting_Controller extends Controller {
private static $allowed_actions = array('HospitalMeetings');
public static $url_handlers = array(
'$Action/$type' => 'HospitalMeetings'
);
public function init() {
parent::init();
if(!Member::currentUser()) {
return $this->httpError(403);
}
}
public function HospitalMeetings(SS_HTTPRequest $request) {
}
}
关于php - Controller 路由在 silverstripe 3.1 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18008462/