LoginSignup
3
3

More than 5 years have passed since last update.

FuelPHPのPaginationクラスをオーバーライドして「次」「前」のリンクに1ページあたりの件数を表示させる。

Posted at

FuelPHPのPaginationクラスは「次のx件」「前のx件」の表示が出来ないので、オーバーライドして、1ページあたりの件数を表示するようにします。

1. 準備

「fuel/core/classes/pagination.php」を「fuel/app/classes/pagination.php」へコピーする。
その後、「fuel/app/bootstrap.php」にてPaginationクラスの使用先を変更するように修正する。

bootstrap.php
......
Autoloader::add_classes (array (
    // Add classes you want to override here
    // Example: 'View' => APPPATH.'classes/view.php',
        'Pagination'    => APPPATH.'classes/pagination.php',
));

// Register the autoloader
Autoloader::register();

2. Paginationクラスの修正

コピーした「fuel/app/classes/pagination.php」を編集する。

pagination.php
<?php
class Pagination extends \Fuel\Core\Pagination
{
    /**
     * Pagination "Previous" link
     *
     * @param    string $value optional text to display in the link
     *
     * @return    string    Markup for the 'previous' page number link
     */
    public function previous($marker = '&laquo;')
    {
        $html = '';

        if ($this->config['total_pages'] > 1)
        {
            if ($this->config['calculated_page'] == 1)
            {
                // {perpage}を$this->config['per_page']に置換するように修正
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}', {perpage}), array('#', $marker, $this->config['per_page']), $this->template['previous-inactive-link']),
                    $this->template['previous-inactive']
                );
                /* // 元のコード
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}'), array('#', $marker), $this->template['previous-inactive-link']),
                    $this->template['previous-inactive']
                );
                */
            }
            else
            {
                $previous_page = $this->config['calculated_page'] - 1;
                $previous_page = ($previous_page == 1) ? '' : $previous_page;
                // {perpage}を$this->config['per_page']に置換するように修正
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}', '{perpage}'), array($this->_make_link($previous_page), $marker, $this->config['per_page']), $this->template['previous-link']),
                    $this->template['previous']
                );
                /* // 元のコード
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}'), array($this->_make_link($previous_page), $marker), $this->template['previous-link']),
                    $this->template['previous']
                );
                */
            }
        }

        return $html;
    }

    /**
     * Pagination "Next" link
     *
     * @param    string $value optional text to display in the link
     *
     * @return    string    Markup for the 'next' page number link
     */
    public function next($marker = '&raquo;')
    {
        $html = '';

        if ($this->config['total_pages'] > 1)
        {
            if ($this->config['calculated_page'] == $this->config['total_pages'])
            {
                // {perpage}を$this->config['per_page']に置換するように修正
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}', '{perpage}'), array('#', $marker, $this->config['per_page']), $this->template['next-inactive-link']),
                    $this->template['next-inactive']
                );
                /* // 元のコード
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}'), array('#', $marker), $this->template['next-inactive-link']),
                    $this->template['next-inactive']
                );
                */
            }
            else
            {
                $next_page = $this->config['calculated_page'] + 1;

                // {perpage}を$this->config['per_page']に置換するように修正
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}', '{perpage}'), array($this->_make_link($next_page), $marker, $this->config['per_page']), $this->template['next-link']),
                    $this->template['next']
                );
                /* // 元のコード
                $html = str_replace(
                    '{link}',
                    str_replace(array('{uri}', '{page}'), array($this->_make_link($next_page), $marker), $this->template['next-link']),
                    $this->template['next']
                );
                */
            }
        }

        return $html;
    }
}

3. 使用方法

「fuel/app/config/pagination.php」でテンプレートを変更する。

pagination.php
<?php
return array(
    'active' => 'perpage',
    'perpage'                     => array(
        'previous-link'           => "\t\t<a href=\"{uri}\" rel=\"prev\">{page} Previous {perpage}</a>\n",
        'previous-inactive-link'  => "\t\t<a href=\"#\" rel=\"prev\">{page} Previous {perpage}</a>\n",
        'next-link'               => "\t\t<a href=\"{uri}\" rel=\"next\">Next {perpage} {page}</a>\n",
        'next-inactive-link'      => "\t\t<a href=\"#\" rel=\"next\">Next {perpage} {page}</a>\n",
    ),
);\
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3