2013-08-02 50 views
3

我正在设置路由到控制器,并且不断收到404或“银河系框架入门”页面。控制器路由不按预期在银条中工作3.1

在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),单纯输出$内容,但当我刷新站点缓存并访问/查看会议/ HospitalMeetings/6?flush = 1

我得到默认的'Silverstripe框架入门'页

我知道routes.yaml路由工作,因为如果我改变路线那里,参观古老的URL,我收到了404,但请求似乎并没有解雇我的$行动......

回答

0

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

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

}

2

您在YAML和控制器中有两条不同的规则($ type vs $ 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) { 
    } 
} 
2

对路由的Silverstripe文档不是在这一点上都清楚,但对于$Action正确解释应该使用双斜杠之前它在routes.yml文件中:

view-meetings//$Action/$type

根据the documentation,这设置了一个叫做'转换点'的东西。具体到什么意思在文档或source code中没有很好地描述,这些URL与规则匹配。