【问题标题】:How do you maintain page number after redirect in CakePHP?在 CakePHP 中重定向后如何维护页码?
【发布时间】:2010-11-06 12:32:19
【问题描述】:

我有一个列出项目的索引视图,它是一个很长的列表,所以我使用 Paginator 将项目限制为 50 个视图。

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

到目前为止一切都很好,但问题是:

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

任何想法都将不胜感激。

【问题讨论】:

  • 一些代码会有所帮助。您可能必须将页码放在“编辑”链接中。

标签: php cakephp pagination http-redirect


【解决方案1】:

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

【讨论】:

  • 这有点像我想的那样,我只是希望 CakePHP 已经内置了更好的方法。谢谢
【解决方案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;
    }

【讨论】:

  • Jason 我没有提到这一点,但我不想为此使用 session,但是您的回答非常好,我仍然会研究它。
  • 我喜欢这个解决方案,因为它适用于所有控制器,因为这是一个非常常见的问题,并且用户希望分页是持久的。
【解决方案3】:

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

在你的控制器或 /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;
  }

希望有帮助!

【讨论】:

  • getPageNumber 可能是性能问题。我不会在大型系统上使用。
【解决方案4】:

做一个简单的

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

有效吗?

【讨论】:

    【解决方案5】:

    使用分页器查看:

    <?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());          
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2011-12-07
      • 1970-01-01
      相关资源
      最近更新 更多