在我的CakePHP App中,我连接了以下路线:
Router::connect('/:city/dealer/:id',
array('controller' => 'dealers', 'action' => 'view'),
array(
'pass' => array('city', 'id'),
'city' => '[a-z]+',
'id' => '[0-9]+'
)
);
这很有效,可以启用: domain.com/washington/dealer/1
但是如何在View的此URL中生成正确的HTML链接?如果我这样做:
echo $this->Html->link(
'Testlink',
array('washington', 'controller' => 'dealers', 'action' => 'view', 1)
);
它将所有参数添加到生成的链接的末尾:
http://domain.com/dealers/view/washington/1
我该如何正确地做到这一点?
答案 0 :(得分:2)
我相信你仍然需要指定params,如下:
echo $this->Html->link('Testlink',
array('controller' => 'dealers', 'action' => 'view', 'city' => 'washington',
'id'=> 1));
Cake在食谱中有一个类似的例子:
<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
// some code here...
}
// routes.php
Router::connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleId in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?php
echo $this->Html->link('CakePHP Rocks', array(
'controller' => 'blog',
'action' => 'view',
'id' => 3,
'slug' => 'CakePHP_Rocks'
));
答案 1 :(得分:0)
嗨塞巴斯蒂安可能为时已晚,无法帮助你,但我可以帮助别人解决这个问题。解决问题的关键是在Helper类中添加url方法。我是通过在View / Helper中创建AppHelper.php来实现的。看起来像这样。我为你的城市更改了我的参数。
查看/助手/ AppHelper.php
<?php
App::uses('Helper', 'View');
class AppHelper extends Helper {
function url($url = null, $full = false) {
if (is_array($url)) {
if (empty($url['city']) && isset($this->params['city'])) {
$url['city'] = $this->params['city'];
}
if (empty($url['controller']) && isset($this->params['controller'])) {
$url['controller'] = $this->params['controller'];
}
if (empty($url['action']) && isset($this->params['action'])) {
$url['action'] = $this->params['action'];
}
}
return parent::url($url, $full);
}
}
?>
然后我创建像
这样的路线Router::connect('/:city/dealer/:id',
array('controller' => 'dealers', 'action' => 'view', 'id'=>':id'),
array('pass' => array('city', 'id'),
'city' => '[a-z]+',
'id' => '[0-9]+'
));
希望这会有所帮助:)