Laravel Custom Presenter / Pagination Class' Illuminate \ Pagination \ Presenter'未找到

时间:2016-03-14 15:40:27

标签: php laravel laravel-5 pagination

嘿伙计我正在创建一个使用https://laravelista.com/laravel-custom-pagination-presenter作为指南的自定义Laravel 5分页演示者,我已按照说明操作,但我的应用程序在查找Illuminate \ Pagination \ Presenter类(我正在扩展)时遇到问题

我在视图中呼叫演示者

视图

 <div class="cf">
    <div class="columns large-6 text-left paginate">

    <?php 
        $presenter = new App\Pagination\Presenters\customPresenter($paginator);

....

我收到的错误是

FatalErrorException in customPresenter.php line 7:
Class 'Illuminate\Pagination\Presenter' not found

我的CustomPresenter类看起来像;

<?php 

namespace App\Pagination\Presenters;

use Illuminate\Pagination\Presenter;

class customPresenter extends Presenter { << error occurs here

/**
 * Get the previous page pagination element.
 *
 * @param  string  $text
 * @return string
 */
public function getPrevious($text = '<i class="fa fa-angle-left"></i>')
{
    // If the current page is less than or equal to one, it means we can't go any
    // further back in the pages, so we will render a disabled previous button
    // when that is the case. Otherwise, we will give it an active "status".
    if ($this->currentPage <= 1)
    {
        return $this->getDisabledTextWrapper($text);
    }

...

我错过了一个包吗?我做了一个作曲家更新并将我的customPresenter类添加到自动加载。

任何帮助非常感谢

欢呼声

2 个答案:

答案 0 :(得分:1)

use语句更改为:

use Illuminate\Contracts\Pagination\Presenter

您还可以扩展其中一个现有的演示者,例如(文件位于App\Extensions文件夹中)。

<?php namespace App\Extensions;

use Illuminate\Pagination\BootstrapThreePresenter;
use Illuminate\Support\HtmlString;

class CustomPagination extends BootstrapThreePresenter {

    public function render()
    {
        if ($this->hasPages()) {
            return new HtmlString(sprintf(
                '<ul class="pagination pagination-sm no-margin pull-right">%s %s %s</ul>',
                $this->getPreviousButton(),
                $this->getLinks(),
                $this->getNextButton()
            ));
        }

        return '';
    }

}

然后您可以使用以下方法在刀片中使用它:

{{ with(new App\Extensions\CustomPagination($data))->render() }}

答案 1 :(得分:0)

在Niraj Shah回答之后,我发现一个很好的提示是在控制器中调用presenter方法:

$data->presenter(function($paginator) {
    return new App\Extensions\CustomPagination($paginator)
});

现在,使用links()或render()的结果是CustomPagination。

注意:$ data是一个Paginator实例。