【问题标题】:Cakephp routing issue for passing parameters from URL从 URL 传递参数的 Cakephp 路由问题
【发布时间】:2013-02-15 22:43:17
【问题描述】:

对于 cakePHP 路由,我明白如果你这样做了

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

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

但是映射http://www.mywebsite.com/controller/id/action 的网址怎么样?

例如:http://www.mywebsite.com/classes/3/create/2

在我的类控制器中的创建函数中,

它将接收参数$id,在本例中为3,$count,在本例中为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}')
);

这对我不起作用。

【问题讨论】:

    标签: cakephp routing controller


    【解决方案1】:

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

    您的路由中还缺少:count,因此如果您通过计数,它将不会匹配。

    您的新路线应如下所示:

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

    【讨论】:

    • 我的get值是这样的/android-atc-training-certification-courses。就在主网址之后。那我怎样才能做到这一点...?
    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2015-10-13
    • 2021-07-16
    • 1970-01-01
    • 2011-12-26
    • 2019-10-09
    • 2012-03-03
    相关资源
    最近更新 更多