控制器路由在silverstripe 3.1中未按预期工作


Controller routing not working as expected in silverstripe 3.1

我正在设置到控制器的路由,我一直得到404或"开始使用silverstripe框架"页面。

在路线上。我有:

---
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时?冲洗=1

我得到了默认的"开始使用Silversstripe框架"页面

我知道routes.yaml中的路由是有效的,因为如果我在那里更改路由并访问旧的URL,我会得到404,但请求似乎不会触发我的$Action。。。

您的YAML和控制器中有两个不同的规则($type和$ID)。此外,我认为您不需要在YAML和Controller中定义路由。

尝试一下,YAML告诉SS将以"查看会议"开头的所有内容发送给您的控制器,然后$url_handlers告诉控制器根据URL中"查看会议"之后的所有内容来处理请求。

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) {
  }
}

关于路由的Silverstepe文档在这一点上根本不清楚,但为了正确解释$Action,您应该在routes.yml文件中使用双斜杠:

view-meetings//$Action/$type

根据文件,这设置了一个叫做"转换点"的东西。无论是在文档中,还是在将URL与规则相匹配的源代码中,都没有很好地描述这到底意味着什么。

我在这里做一些猜测,但如果你放弃怎么办

public static $url_handlers = array(
    'view-meetings/$Action/$ID' => 'HospitalMeetings'
);

部分,并将操作方法更改为:

// View a list of Hospital meetings of a specified type for this
public function HospitalMeetings(SS_HTTPRequest $request) {
// Should print 8 if url is /view-meetings/HospitalMeetings/6
print_r($request->param('type');

}