从URL

2013-02-15 35 views
1

对于CakePHP的路由传递参数CakePHP的路由问题,据我所知,如果你做从URL

Router::connect(
    '/:controller/:id', 
    array('action' => 'view'), 
    array('id' => '[0-9]+') 
); 

这将映射到是http://www.mywebsite.com/controller/view/id任何网址,

但如何映射URL是http://www.mywebsite.com/controller/id/action

对于前:http://www.mywebsite.com/classes/3/create/2

,在我创建的类控制器功能,

它将接收参数的$ id,这是3在这种情况下,和$算,也就是2在这种情况下,

public function create($id, $count) { 
    .... 
    // i can here create a total number of $count students 
    // and assign them class_id $id 

    // so student1.class_id = 3 
    // and student2.class_id = 3 
} 

我试过,

Router::connect(
'/:controller/:id/:action', 
    array('id' => '[0-9]+'), 
    array('count' => '[0-9]{,2}') 
); 

它没有活像k对我来说。

回答

0

为了使Cake传递id和计数到您的控制器动作,您必须让路由知道他们传递了参数。为此,请按照您希望传递的顺序将它们包含在最后一个选项的pass数组中。

您还在您的路线中缺少:count,因此如果您通过计数,则不会匹配。

您的新路线应该是这样的:

Router::connect(
    '/:controller/:id/:action/:count', 
    array(), 
    array(
    'pass' => array('id', 'count'), 
    'id' => '[0-9]+', 
     'count' => '[0-9]{,2}' 
) 
); 
+0

我获取价值是这样的/ Android的ATC培训认证课程。主URL后面。那么我怎么能做到这一点......? – GYaN 2017-07-10 06:09:37