CakePHP: How to route Pagination's sort parameters?
So I'm trying to page items on my index page using the paginator and custom routes. It's all through the index action, but the index action can show items sorted by newest, votes, active or views. Right now, the URL looks like t开发者_如何学Chis:
items/index/sort:created/direction:desc
And if you aren't on page one, it looks like this:
items/index/sort:created/direction:desc/page:2
I'd like to use the router to have it look like this:
newest/
I can get that far with this route:
Router::connect(
'/newest/*',
array('controller'=>'items', 'action'=>'index', 'sort'=>'created', 'direction'=>'desc')
);
However, the pager links don't follow the route. As soon as you click next page, you're back to:
items/index/sort:created/direction:desc/page:2
How can I make this follow the router and give me what I want? Keep in mind, it's all from the same controller action, I'm trying to route the sort parameters of pagination basically.
For me your code is working (I've tested your example). Have you done something unusual with the paginator helper?
Here is my Routes:
Router::connect('/newest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'desc'));
Router::connect('/oldest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'asc'));
And here are the urls which I've seen when I sort by age column:
http://localhost/cakephp/1.3.0/newest/page:1
http://localhost/cakephp/1.3.0/newest/page:2
http://localhost/cakephp/1.3.0/newest/page:3
And oldest:
http://localhost/cakephp/1.3.0/oldest/page:1
http://localhost/cakephp/1.3.0/oldest/page:2
http://localhost/cakephp/1.3.0/oldest/page:3
And it's working with all links in the pager (first, prev, 1,2,3 next, last).
You want to include the passed args I think. Something like this,
$this->params = $this->passedArgs();
Have a check here also, http://book.cakephp.org/view/46/Routes-Configuration
Otherwise I would extend the HTML Helper to create my own link method which read in the parameters from the url and created a link accordingly. Then you could manage your own links from your own helper :)
Don't forget that you need to have checks in the index action to deal with this. Personally I would be far more inclined to create an action in the controller for each of these.
function newest(){
}
function votes(){
}
function active(){
}
//etc
精彩评论