更改CakePhp1.3分页器目标URL? [英] change CakePhp1.3 paginator destination url?

查看:69
本文介绍了更改CakePhp1.3分页器目标URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cakephp的新手...并且我的网页上带有以下网址:

I'm new to cakephp...and I have a page with a url this:

http://localhost/books/filteredByAuthor/John-Doe

所以控制器是"books",操作是"filteredByAuthor",而"John-Doe"是一个参数..但URL看起来很丑,所以我添加了如下所示的Route:

so the controller is ´books´, the action is ´filteredByAuthor´ and ´John-Doe´ is a parameter.. but the url looks ugly so i've added a Route like this:

Router::connect('/author/:name', array( 'controller' => 'books','action' => 'filteredByAuthor'), array('pass'=>array('name'),'name'=>".*"));

现在我的链接是:

http://localhost/author/John-Doe

问题是该视图具有分页器,并且当我更改页面时(通过单击下一个"或上一个"按钮).分页器不会考虑我的路线...并且会将网址更改为此

the problem is that the view has a paginator and when i change the page (by clicking on the next or prev button).. the paginator won't consider my routing... and will change the url to this

http://localhost/books/filteredByAuthor/John-Doe/page:2

我认为代码只是:

<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>

文档没有说明如何避免这种情况,我花了数小时阅读分页器的源代码和api ..最后,我只希望我的链接像这样:(包括排序和方向)在网址上

the documentation doesn't say anything about avoiding this and i've spent hours reading the paginators source code and api.. and in the end i just want my links to be something like this: (with the sort and direction included on the url)

http://localhost/author/John-Doe/1/name/asc

有可能这样做吗?

推荐答案

讨厌回答我自己的问题...但这可能会节省一些时间给另一个开发人员=)(都是为了获得良好的业力)

hate to answer my own question... but this might save some time to another developper =) (is all about getting good karma)

我发现可以将"options"数组传递给分页器,并且在该数组内可以指定分页器将用于创建链接的url(由控制器,操作和参数组成的数组).因此,您必须在routes.php文件中写入所有可能的路由.基本上有3种可能性:

i found out that you can pass an "options" array to the paginator, and inside that array you can specify the url (an array of: controller, action and parameters) that the paginator will use to create the links.. so you have to write all the possible routes in the routes.php file. Basically there are 3 possibilities:

  • 未定义页面"时

例如:

http://localhost/author/John-Doe

分页器将假定它是第一页.相应的路线为:

the paginator will assume that the it's the first page. The corresponding route would be:

Router::connect('/author/:name', array( 'controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name'),'name'=>'[a-zA-Z\-]+'));

  • 定义页面"时
  • 例如:

    http://localhost/author/John-Doe/3   (page 3)
    

    路线为:

    Router::connect('/author/:name/:page', array( 'controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name','page'),'name'=>'[a-zA-Z\-]+','page'=>'[0-9]+'));
    

    • 最后,当页面和排序在url上定义时(通过点击 sort 链接).
      • finally when the page and the sort is defined on the url (by clicking on the sort links created by the paginator).
      • 例如:

        http://localhost/author/John-Doe/3/title/desc   (John Doe's books ordered desc by title)
        

        路线为:

        Router::connect('/author/:name/:page/:sort/:direction', array( 'controller' => 'books','action' => 'filteredByAuthor'),
                    array('pass'=>array('name','page','sort','direction'),
                    'name'=>"[a-zA-Z\-]+",
                    'page'=>'[0-9]*',
                    'sort'=>'[a-zA-Z\.]+',
                    'direction'=>'[a-z]+',
                    ));
        

        在视图上,您​​必须取消设置分页器创建的url,因为您将在控制器上指定自己的url数组:

        on the view you have to unset the url created by the paginator, cause you'll specify your own url array on the controller:

        控制器:

        function filteredByAuthor($name = null,$page = null , $sort = null , $direction = null){
        $option_url = array('controller'=>'books','action'=>'filteredByAuthor','name'=>$name);
        if($sort){
            $this->passedArgs['sort'] = $sort;
            $options_url['sort'] = $sort;
        }
        if($direction){
            $this->passedArgs['direction'] = $direction;
            $options_url['direction'] = $direction;
        }
        

        使用set()将变量$options_url发送到视图...因此在视图中,您需要执行以下操作:

        Send the variable $options_url to the view using set()... so in the view you'll need to do this:

        查看:

        unset($this->Paginator->options['url']);
        echo $this->Paginator->prev(__('« Précédente', true), array('url'=>$options_url), null, array('class'=>'disabled'));
        echo $this->Paginator->numbers(array('separator'=>'','url'=>$options_url));
        echo $this->Paginator->next(__('Suivante »', true), array('url'=>$options_url), null, array('class' => 'disabled'));
        

        现在,在排序链接上,您需要取消设置变量"sort"和"direction".我们已经使用它们在分页器上创建链接,但是如果不删除它们,则sort()函数将使用它们...并且我们将无法对=)

        Now, on the sort links you'll need to unset the variables 'sort' and 'direction'. We already used them to create the links on the paginator, but if we dont delete them, then the sort() function will use them... and we wont be able to sort =)

        $options_sort = $options_url;
        unset($options_sort['direction']);
        unset($options_sort['sort']);
        echo $this->Paginator->sort('Produit <span>&nbsp;</span>', 'title',array('escape'=>false,'url'=>$options_sort)); 
        

        希望这有帮助=)

        这篇关于更改CakePhp1.3分页器目标URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆