【问题标题】:Controller routing not working as expected in silverstripe 3.1控制器路由在 silverstripe 3.1 中未按预期工作
【发布时间】:2013-08-02 03:47:19
【问题描述】:

我正在设置到控制器的路由,但我不断收到 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...

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

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

    试试这个,YAML 告诉 SS 将以“view-meetings”开头的所有内容发送到您的控制器,然后$url_handlers 告诉控制器如何处理请求,具体取决于 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) {
      }
    }
    

    【讨论】:

      【解决方案2】:

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

      view-meetings//$Action/$type

      根据the documentation,这设置了一个叫做“移位点”的东西。这意味着什么在文档或source code 中都没有很好地描述,它将 URL 与规则匹配。

      【讨论】:

        【解决方案3】:

        我在这里做一些猜测,但是如果你放弃了

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

        part 并将 Action 方法更改为:

        // 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');
        

        }

        【讨论】:

          猜你喜欢
          • 2020-10-06
          • 2014-09-20
          • 2014-05-02
          • 2016-08-19
          • 2015-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多