在cakephp 3中创建分页器回收组件

时间:2018-08-02 18:19:19

标签: php cakephp-3.x

下面是我在分页调用组件中的代码。我必须与上一个页面控制器名称的动作名称和页面编号相同,以便如果我要查看编辑或分页中的任何动作,它将重定向到上一个页面。下面我的代码在cakephp2.x中正常工作,但在cakephp3.x中不工作。   我必须在哪里进行修改?

<?php
   namespace App\Controller\Component;
    use Cake\Controller\Component;

     class PaginationRecallComponent extends Component {
     const PREV_DATA_KEY = 'Paginaion-PrevData';

public $components = array('Session');
private $_controller = NULL;
private $_action = NULL;
private $_previousUrl;

public function initialize(Controller $controller)
{
    $this->_controller = $controller;
    $this->_action = $controller->params['action'];
}
  public function startup(Controller $controller)
{
    if ($this->_controller->name === 'CakeError')
        return;

    $this->_restorePagingParams();

    // save the current controller and action for the next time
    $this->Session->write(
        self::PREV_DATA_KEY,array
        (
            'controller' => $this->_controller->name,
            'action' => $this->_action
        )
    );
}
         private function _restorePagingParams()
{
    $sessionKey = "Pagination.{$this->_controller->name}.{$this->_action}";

    // extract paging data from the request parameters
    $pagingParams = $this->_extractPagingParams();

    // if paging data exist write them in the session
    if (!empty($pagingParams)) {
        $this->Session->write( $sessionKey, $pagingParams);
        return;
    }

    // no paging data.
    // construct the previous URL
    $this->_previousUrl = $this->Session->check(self::PREV_DATA_KEY)
        ? $this->Session->read(self::PREV_DATA_KEY)
        : array(
            'controller' => '',
            'action' => ''
        );

    // and check if the current page is the same as the previous
    if ($this->_previousUrl['controller'] === $this->_controller->name &&
        $this->_previousUrl['action'] === $this->_action) {
        // in this case we have a link from our own paging::numbers() function
        // to move to page 1 pf the current page, delete any paging data
        $this->Session->delete($sessionKey);
        return;
    }

    // we are comming from a different page so if we have any session data
    if ($this->Session->check($sessionKey))
        // then restore and use them
        $this->_controller->request->params['named'] = array_merge(
            $this->_controller->request->params['named'],
            $this->Session->read($sessionKey)
        );
}
      private function _extractPagingParams()
{
    $pagingParams = $this->_controller->request->params['named'];
    $vars = array('page', 'sort', 'direction');
    $keys = array_keys($pagingParams);
    $count = count($keys);

    for ($i = 0; $i < $count; $i++)
        if (!in_array($keys[$i], $vars))
            unset($pagingParams[$keys[$i]]);

    return $pagingParams;
}

0 个答案:

没有答案