CakePHP:如何路由分页的排序参数?

发布于 2024-09-30 10:14:17 字数 691 浏览 0 评论 0 原文

因此,我尝试使用分页器和自定义路由对索引页面上的项目进行分页。这一切都是通过索引操作完成的,但索引操作可以显示按最新、投票、活跃或视图排序的项目。现在,URL 看起来像这样:

items/index/sort:created/direction:desc

如果您不在第一页,它看起来像这样:

items/index/sort:created/direction:desc/page:2

我想使用路由器让它看起来像这样:

newest/

我可以通过这条路线到达那么远:

  Router::connect(
    '/newest/*',
    array('controller'=>'items', 'action'=>'index', 'sort'=>'created', 'direction'=>'desc')
);

但是,寻呼机链接不遵循该路线。一旦你点击下一页,你就会回到:

items/index/sort:created/direction:desc/page:2

我怎样才能让它跟随路由器并给我我想要的东西?请记住,这一切都来自相同的控制器操作,我基本上正在尝试路由分页的排序参数。

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 this:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-10-07 10:14:18

对我来说,你的代码正在工作(我已经测试了你的示例)。您是否使用分页器助手做了一些不寻常的事情?

这是我的路线:

Router::connect('/newest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'desc'));
Router::connect('/oldest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'asc'));

这是我按年龄列排序时看到的网址:

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

最旧的:

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

它适用于寻呼机中的所有链接(第一个、上一个、1、2、3、下一个、最后一个)。

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).

蓝梦月影 2024-10-07 10:14:18

我认为你想包含传递的参数。像这样的东西,

$this->params = $this->passedArgs();

也在这里检查一下, http://book.cakephp.org/view /46/Routes-Configuration

否则,我将扩展 HTML Helper 以创建我自己的链接方法,该方法从 url 读取参数并相应地创建链接。然后你可以从你自己的助手管理你自己的链接:)

不要忘记你需要检查索引操作来处理这个问题。就我个人而言,我更倾向于在控制器中为每一个创建一个操作。

function newest(){

}
function votes(){

}
function active(){

}

//etc

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文