代码之家  ›  专栏  ›  技术社区  ›  Daniel Bingham

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

  •  3
  • Daniel Bingham  · 技术社区  · 15 年前

    所以我尝试使用分页器和自定义路由来分页索引页上的项目。这都是通过索引操作完成的,但是索引操作可以显示按最新、投票、活动或视图排序的项。现在,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')
    );
    

    然而,寻呼机链接并不是按照路线走的。单击“下一页”后,您将返回:

    项目/索引/排序:创建/方向:描述/页面:2
    

    2 回复  |  直到 15 年前
        1
  •  3
  •   Nik Chankov    15 年前

    对我来说,你的代码正在工作(我已经测试过你的例子)。你有没有和那个异教徒帮手做过不寻常的事?

    这是我的路线:

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

    下面是我在按年龄列排序时看到的URL:

    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
    

    它可以处理寻呼机中的所有链接(first、prev、1、2、3 next、last)。

        2
  •  0
  •   David Yell    15 年前

    我想你应该包括通过的参数。像这样的东西,

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

    http://book.cakephp.org/view/46/Routes-Configuration

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

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

    function newest(){
    
    }
    function votes(){
    
    }
    function active(){
    
    }
    
    //etc
    
    推荐文章