【问题标题】:cakephp 2.4: pass value instead of id in url?cakephp 2.4:在 url 中传递值而不是 id?
【发布时间】:2014-02-21 13:00:07
【问题描述】:

我是 cakephp 新手,想知道如何更改这样的网址

C:/xyz.com/countries/view/5 到 C:/xyz.com/countries/ABC

其中 5 是 ABC 国家/地区的 id

提前致谢

【问题讨论】:

    标签: cakephp-2.4


    【解决方案1】:
    Router::connect('/countries/:country_id', array('controller' => 'countries', 'action' => 'view'), array('pass' => array('country_id')));
    

    在控制器中:

    public function view() {
       $countryId =  $this->request->params['country_id'];
    }
    

    【讨论】:

    • 做了同样的事情,使用了公共函数 view($id=null){$countryId=$this->request->params['id']; ... ...} 并添加 Router::connect('/countries/:id', array('controller' => 'countries', 'action' => 'view'), array('pass' =>数组('id')));在 routes.php 文件中但给了我错误通知 (8): Undefined index: id in this line $countryId = $this->request->params['id'];并且没有对网址进行任何更改..
    • 它对我不起作用,给出这个通知(8):未定义索引:id in this line $countryId = $this->request->params['id'];如果我使用 $countryId = $this->request->params['pass'][0];然后没有收到通知,但需要'id'
    • debug($this->request->params); 看看你得到了什么。然后使用那个变量
    • 获取数组('plugin' => null, 'controller' => 'countries', 'action' => 'view', 'named' => array(), 'pass' =>数组((int) 0 => '1'))
    • 如果你完全按照答案使用它,它会发送$country_id 正确的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多