[英]Unable to set named parameter with CakePHP
I'm using the CakePHP pagination helper with a custom route however it seems to be ignoring the page number. 我正在使用带有自定义路由的CakePHP分页助手,但是它似乎忽略了页码。 I believe this is because the named parameter
page
doesn't exist in the request, which I've verified using debug($this->request->params);
我相信这是因为命名参数
page
在请求中不存在,已使用debug($this->request->params);
: :
array(
'plugin' => null,
'controller' => 'things',
'action' => 'index',
'named' => array(),
'pass' => array(),
'page' => '2'
)
As you can see, it's putting page
outside of the named
array. 如您所见,它将
page
放置在named
数组之外。 This is the route I've defined for the custom pagination URLs: 这是我为自定义分页网址定义的路由:
Router::connect('/things/:page', array('controller' => 'skins', 'action' => 'index'), array('page' => '[0-9]+'));
Obviously, this means that no matter what page number I click on only the first results show. 显然,这意味着无论我单击哪个页码,都只会显示第一个结果。
How can I modify the route to insert the page into the named parameters array properly? 如何修改路由以将页面正确插入命名参数数组? I'd rather not directly modify
$this->request->params['named']
from the controller directly. 我宁愿不直接从控制器直接修改
$this->request->params['named']
。
According to the awesome documentation in the CakePHP Book: 根据CakePHP书中的出色文档:
If you are planning to use custom named arguments with your route, you have to make the router aware of it using the Router::connectNamed() function.
如果计划在路由中使用自定义命名参数,则必须使用Router :: connectNamed()函数使路由器知道它。 So if you want the above route to match urls like /cooks/some_action/type:chef we do:
因此,如果您希望上述路线匹配/ cooks / some_action / type:chef之类的网址,我们可以这样做:
Router::connectNamed(array('type'));
Router::connect(
'/cooks/:action/*', array('controller' => 'users')
);
http://book.cakephp.org/2.0/en/development/routing.html#routes-configuration http://book.cakephp.org/2.0/en/development/routing.html#routes-configuration
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.