Cakephp:paginator和json数组作为控制器参数的问题

时间:2014-09-06 20:39:48

标签: json cakephp pagination

我有控制器的功能"显示"期待用JSON编码的数组:

public function display($json_data){
pr($this->passedArgs);
    $matches = json_decode($json_data,true);
    $ids = array_keys($matches);

    $this->Version->recursive = 0;
    $this->Paginator->settings = $this->paginate;

    $name='Versions';
    $class='Version';
    $items = $this->Paginator->paginate(null,array("`".$class."`.id"=>$ids));
    $fields= $this->Version->schema();
    $ignore=array('');
    $this->set(compact('items','fields','name','class','ignore','matches'));
}

在display.ctp视图中,我有一些非常类似于食谱示例的内容,例如:

<?php 
    echo '<li>'.$this->Paginator->prev('&#171', array('tag'=>'div','class' => 'pure-button prev','escape'=>false), null, array('tag'=>'div','class' => 'pure-button prev','escape'=>false)).'</li>';
    echo '<li>'.$this->Paginator->numbers(array('separator' => '','class'=>'pure-button','currentClass'=>'pure-button pure-button-active')).'</li>';
    echo '<li>'.$this->Paginator->next('&#187', array('tag'=>'div','class' => 'pure-button next','escape'=>false), null, array('tag'=>'div','class' => 'pure-button next','escape'=>false)).'</li>';
?>

阵列&#39; $匹配&#39;是应该作为控制者输入参数的人。

更确切地说,第一次打电话给&#34;显示&#34;方法,从另一个视图而不是display.ctp,数组&#39;匹配&#39;在JSON中进行了最终编码,处理是正确的。 保留数组并将其发送到display.ctp视图。当我点击分页器控件的下一个或上一个链接时,发送到控制器的数组会被编码。 看起来像人物&#34;%25&#34;被转换为&#34;%&#34;他们不应该这样做:它就像json字符串上的html编码问题并且使它错误。 我收到错误:

Error: The requested address /application/versions/display/%7B%223%22%3A%5B%22name%22%5D%2C%224%22%3A%5B%22name%22%5D%2C%225%22%3A%5B%22name%22%5D%2C%226%22%3A%5B%22name%22%5D%2C%227%22%3A%5B%22name%22%5D%2C%228%22%3A%5B%22name%22%5D%2C%229%22%3A%5B%22name%22%5D%2C%2210%22%3A%5B%22name%22%5D%2C%2211%22%3A%5B%22name%22%5D%2C%2212%22%3A%5B%22name%22%5D%2C%2213%22%3A%5B%22name%22%5D%2C%2214%22%3A%5B%22name%22%5D%2C%2215%22%3A%5B%22name%22%5D%2C%2216%22%3A%5B%22name%22%5D%2C%2217%22%3A%5B%22name%22%5D%2C%2218%22%3A%5B%22name%22%5D%2C%2219%22%3A%5B%22name%22%5D%2C%2220%22%3A%5B%22name%22%5D%2C%2221%22%3A%5B%22name%22%5D%2C%2222%22%3A%5B%22name%22%5D%2C%2223%22%3A%5B%22name%22%5D%2C%2224%22%3A%5B%22name%22%5D%7D/page:2' was not found on this server.

如果我查看工作字符串(显示视图之前),我得到了:

    http://nas/application/versions/display/%257B%25223%2522%253A%255B%2522name%2522%255D%252C%25224%2522%253A%255B%2522name%2522%255D%252C%25225%2522%253A%255B%2522name%2522%255D%252C%25226%2522%253A%255B%2522name%2522%255D%252C%25227%2522%253A%255B%2522name%2522%255D%252C%25228%2522%253A%255B%2522name%2522%255D%252C%25229%2522%253A%255B%2522name%2522%255D%252C%252210%2522%253A%255B%2522name%2522%255D%252C%252211%2522%253A%255B%2522name%2522%255D%252C%252212%2522%253A%255B%2522name%2522%255D%252C%252213%2522%253A%255B%2522name%2522%255D%252C%252214%2522%253A%255B%2522name%2522%255D%252C%252215%2522%253A%255B%2522name%2522%255D%252C%252216%2522%253A%255B%2522name%2522%255D%252C%252217%2522%253A%255B%2522name%2522%255D%252C%252218%2522%253A%255B%2522name%2522%255D%252C%252219%2522%253A%255B%2522name%2522%255D%252C%252220%2522%253A%255B%2522name%2522%255D%252C%252221%2522%253A%255B%2522name%2522%255D%252C%252222%2522%253A%255B%2522name%2522%255D%252C%252223%2522%253A%255B%2522name%2522%255D%252C%252224%2522%253A%255B%2522name%2522%255D%257D

我做错了什么或遗失了什么?

萨姆

1 个答案:

答案 0 :(得分:0)

所以,只是为了结束话题:我找到了答案:

我只需要重新编码之前将其传递给paginator的url,然后强制将数组作为参数:

    <?php 

        $matches_json = urlencode(json_encode($matches));
        echo '<li>'.$this->Paginator->prev('&#171', array('url'=>array($matches_json),'tag'=>'div','class' => 'pure-button prev','escape'=>false), null, array('tag'=>'div','class' => 'pure-button prev','escape'=>false)).'</li>';
        echo '<li>'.$this->Paginator->numbers(array('separator' => '','class'=>'pure-button','currentClass'=>'pure-button pure-button-active')).'</li>';
        echo '<li>'.$this->Paginator->next('&#187', array('url'=>array($matches_json),'tag'=>'div','class' => 'pure-button next','escape'=>false), null, array('tag'=>'div','class' => 'pure-button next','escape'=>false)).'</li>';
    ?>