LoginSignup
2
1

laravel ページネーションの「pagination.previous」と「pagination.next」はどこに記載されている?

Last updated at Posted at 2022-10-13

概要

  • laravelのページネーションにて特に言語ファイルを作成しない状態でlinks()関数を用いてページネーションリンクを設定すると表示される「pagination.previous」と「pagination.next」はどこで定義されているのか追ってみた。

追っかけてみる。

  1. まずlinks()関数の定義部分を見てみる。vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.phpの77行目付近で下記の様に定義されていた。

    vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php
    /**
     * Render the paginator using the given view.
     *
     * @param  string|null  $view
     * @param  array  $data
     * @return \Illuminate\Contracts\Support\Htmlable
     */
    public function links($view = null, $data = [])
    {
        return $this->render($view, $data);
    }
    

 1. links()ではなさそうだった。ここはただ同じクラス内のrender()を呼んでいるだけっぽい。そのrender()を見てみる。

```vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php
/**
 * Render the paginator using the given view.
 *
 * @param  string|null  $view
 * @param  array  $data
 * @return \Illuminate\Contracts\Support\Htmlable
 */
public function render($view = null, $data = [])
{
    return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
        'paginator' => $this,
        'elements' => $this->elements(),
    ]));
}
```
  1. render()では表示内容やらなにやらをビルドしている模様だった。キーpaginatiorに自分自身のオブジェクト($this)を渡しているっぽい。「pagination.previous」と「pagination.next」は今のクラス内もしくは継承元のクラスで定義されているはずだ。

  2. すぐ下のlinkCollection()関数で定義されていた。

    vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php
    /**
     * Get the paginator links as a collection (for JSON responses).
     *
     * @return \Illuminate\Support\Collection
     */
    public function linkCollection()
    {
        return collect($this->elements())->flatMap(function ($item) {
            if (! is_array($item)) {
                return [['url' => null, 'label' => '...', 'active' => false]];
            }
    
            return collect($item)->map(function ($url, $page) {
                return [
                    'url' => $url,
                    'label' => (string) $page,
                    'active' => $this->currentPage() === $page,
                ];
            });
        })->prepend([
            'url' => $this->previousPageUrl(),
            'label' => function_exists('__') ? __('pagination.previous') : 'Previous', // ここ!!
            'active' => false,
        ])->push([
            'url' => $this->nextPageUrl(),
            'label' => function_exists('__') ? __('pagination.next') : 'Next', // ここ!!
            'active' => false,
        ]);
    }
    
  3. どうやら表示ラベルとして__()関数が定義されている時は__('pagination.previous')__('pagination.next')が実行されるようになっているっぽい。

  4. resources/lang/ja/pagination.phpを作ってなかったため__()関数の引数である「pagination.previous」と「pagination.next」がそのまま表示されていたっぽい。(config/app.phplocalejaの場合)

  5. ページネーションの「ページ戻り」や「ページ送り」のボタンの表示名を変更したい場合、当該の関数をオーバーライドするのではなく、resources/lang/ja/pagination.phpの中に配列でキーpreviousとキーnextに紐づく値を設定するほうが良いと思う。

vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php
118行目

2
1
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
2
1