在CakePHP中重定向后如何维护页码?

时间:2009-06-28 17:45:15

标签: php cakephp pagination http-redirect

我有一个列出项目的索引视图,这是一个很长的列表,因此我使用Paginator将项目限制为50到一个视图。

每个项目都有一个“编辑”链接,该链接转到带有输入/验证/等的编辑视图。提交该表单后,我将使用重定向回索引视图。

到目前为止一切都那么好,但这就是问题:

如果用户在索引的第N页上并且他们单击编辑和编辑项目,我希望将它们重定向回索引的页面N.如果我知道页码,我可以将“/ page:N”粘贴到URL的末尾,但我不知道如何获取页码。 (N可以是任何页码,但尤其是> = 2)

任何想法都会受到赞赏。

5 个答案:

答案 0 :(得分:2)

页码应该是列表视图中$ params var的一部分。只需将其添加到编辑链接的末尾并从那里处理它。在编辑页面上,您需要一种方法来接收可选页码,在任何表单提交期间存储它,并转发回具有相同页码的列表。

答案 1 :(得分:2)

我创建了一个在会话中保存页面的组件。然后在app_controller.php中,我检查会话中是否有正在使用的特定模型的内容,然后将其添加到url。如果您对组件的代码感兴趣,请给我留言。如果用户在编辑之前更改了索引页面中的排序顺序,我也会存储订单。

请看这里的来源: http://github.com/jimiyash/cake-pluggables/blob/a0c3774982c19d02cfdd19a2977eabe046a4b294/controllers/components/memory.php

这是我正在做的事情的要点。

//controller or component code
if(!empty($params['named']) && !empty($params['controller']) && $params['action'] == 'admin_index'){
    $this->Session->write("Pagem.{$params['controller']}", $params['named']);
}

//app_controller.php
    $redirectNew = "";
    if(is_array($redirectTo)){
        if(!empty($params['prefix']) && $params['prefix'] == 'admin'){
            $redirectNew .= '/admin';
        }
        if(!empty($params['controller'])){
            $redirectNew .= "/" . $params['controller'];
        }
        if(!empty($redirectTo['action'])){
            $redirectNew .= "/" . $redirectTo['action'];
        }
    } else {
        $redirectNew = $redirectTo;
    }

    $controller = $params['controller'];
    if($this->Session->check("Pagem.$controller")){
        $settings =  $this->Session->read("Pagem.$controller");
        $append = array();
        foreach($settings as $key=>$value){
            $append[] = "$key:$value";
        }
        return $redirectNew . "/" . join("/", $append);
    } else {
        return $redirectNew;
    }

答案 2 :(得分:2)

如果我理解正确,上面的内容适合编辑,但不适合添加。此解决方案应适用于以下两种情况:

在您的控制器或/app/app_controller.php中,添加以下内容:

$insertID = $this->{$this->modelClass}->getLastInsertID();
$page = $this->{$this->modelClass}->getPageNumber($insertID, $this->paginate['limit']);
$this->redirect("/admin/{$controllerName}/index/page:{$page}");

......以及类似的内容用于编辑:

$page = $this->{$this->modelClass}->getPageNumber($id, $this->paginate['limit']);
$this->redirect("/admin/{$controllerName}/index/page:{$page}");

在你的/app/app_model.php中,输入:

/**
 * Work out which page a record is on, so the user can be redirected to
 * the correct page.  (Not necessarily the page she came from, as this
 * could be a new record.)
 */

  function getPageNumber($id, $rowsPerPage) {
    $result = $this->find('list'); // id => name
    $resultIDs = array_keys($result); // position - 1 => id
    $resultPositions = array_flip($resultIDs); // id => position - 1
    $position = $resultPositions[$id] + 1; // Find the row number of the record
    $page = ceil($position / $rowsPerPage); // Find the page of that row number
    return $page;
  }

希望有所帮助!

答案 3 :(得分:1)

做一个简单的

$this->redirect($this->referer());

作品?

答案 4 :(得分:0)

使用paginator:

<?php
if ($this->Paginator->hasPage(null, 2)) {   
$pag_Start = $this->Paginator->counter('{:start}');
$pag_End = $this->Paginator->counter('{:end}');
if( $pag_Start == $pag_End ){
$pageToRedirect = $this->Paginator->current('Posts');
}else{
$pageToRedirect= '';
}}?>

然后链接到编辑页面

<?php
echo $this->Form->postLink(
'Edit',
array('action' => 'edit', $subscription['Post']['id']));
?>

在控制器中:

public function edit($post_id, $pageToRedirect = false){

    //after all editing its done redirect

    if($pageToRedirect){
    // if record was last in pagination page redirect to previous page
    $pageToRedirect = $pageToRedirect -1;
    return $this->redirect(array('action' => 'index/page:'.$pageToRedirect ));
    }else{
    // else redirect to the same pagination page
    $this->redirect($this->referer());          
    }

}