代码之家  ›  专栏  ›  技术社区  ›  Matthew Groves

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

  •  1
  • Matthew Groves  · 技术社区  · 17 年前

    5 回复  |  直到 13 年前
        1
  •  2
  •   Mike B    17 年前

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

        2
  •  2
  •   jimiyash    17 年前

    我创建了一个将页面保存在会话中的组件。然后在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;
        }
    
        3
  •  2
  •   user266140 user266140    16 年前

    $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}");
    

    /**
     * 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;
      }
    

    希望这能有所帮助!

        4
  •  1
  •   Paolo    15 年前

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

    作品?

        5
  •  0
  •   d78    11 年前

    使用分页器查看:

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