PHP-控制器路由未按预期在Silverstripe 3.1中工作
作者:互联网
我正在设置到控制器的路由,并且不断收到404或“ silverstripe框架入门”页面.
在routes.yaml中,我有:
---
Name: nzoaroutes
After: framework/routes#coreroutes
---
Director:
rules:
'view-meetings/$Action/$type': 'ViewMeeting_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和控制器中有2条不同的规则($type与$ID).另外,我认为您不需要在YAML和Controller中都定义路由.
尝试此操作,YAML告诉SS将以’view-meetings’开头的所有内容发送到Controller,然后$url_handlers告诉Controller处理请求,具体取决于URL中’view-meetings’之后的所有内容.
routes.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) {
}
}
标签:silverstripe,php 来源: https://codeday.me/bug/20191030/1969033.html