Php 使用Slim 3使用Elount-5.3分页

Php 使用Slim 3使用Elount-5.3分页,php,pagination,eloquent,Php,Pagination,Eloquent,以下是我的分页代码: 控制器: public function index($request, $response) { $current_page = $request->getParam('page'); Paginator::currentPageResolver(function() use ($current_page) { return $current_page; }); $routes = $this->route

以下是我的分页代码:

控制器:

public function index($request, $response) {

    $current_page = $request->getParam('page');

    Paginator::currentPageResolver(function() use ($current_page) {
        return $current_page;
    });

    $routes = $this->route->paginate(2);

    return $this->view->render($response, 'dashboard/dashboard.twig', [
        'routes' => $routes
    ]);
}
{% for route in routes %}
    {{ route.route_name }}<br>
{% endfor %}
查看:

public function index($request, $response) {

    $current_page = $request->getParam('page');

    Paginator::currentPageResolver(function() use ($current_page) {
        return $current_page;
    });

    $routes = $this->route->paginate(2);

    return $this->view->render($response, 'dashboard/dashboard.twig', [
        'routes' => $routes
    ]);
}
{% for route in routes %}
    {{ route.route_name }}<br>
{% endfor %}
它提示我一个错误

消息:在null上调用成员函数make()

文件:C:\xampp\htdocs…\vendor\illumb\pagination\LengthAwarePaginator.php

更新:

public function index($request, $response) {

    $current_page = $request->getParam('page');

    Paginator::currentPageResolver(function() use ($current_page) {
        return $current_page;
    });

    $routes = $this->route->paginate(2);

    return $this->view->render($response, 'dashboard/dashboard.twig', [
        'routes' => $routes
    ]);
}
{% for route in routes %}
    {{ route.route_name }}<br>
{% endfor %}
当我尝试回显控制器中的
links()
方法时。我发现了这个错误

$routes = $this->route->paginate(5);
echo $routes->links();
die();
警告:call_user_func()要求参数1为有效回调,在第377行的C:\xampp\htdocs…\vendor\illumb\pagination\AbstractPaginator.php中未给出数组或字符串

然后我检查了源代码,就是这个

/**
 * Get an instance of the view factory from the resolver.
 *
 * @return \Illuminate\Contracts\View\Factory
 */
public static function viewFactory()
{
    return call_user_func(static::$viewFactoryResolver);
}
有什么解决办法吗

参考此链接,此方法在5.2中有效


但不幸的是,它不再工作了。

作为临时解决方案,我似乎可以访问paginate对象的方法。所以我试着在互联网上找到一个分页的laravel模板

这是我想出的解决办法

它所做的是,它仅在数组不是空的情况下显示,显然,如果它呈现了多个页面,它就会显示。(取决于将在分页中设置的页数)

还有我从某处得到的模板,带有适合我需要的修改过的函数

{% if arrays is not empty and arrays.lastPage > 1 %}

    <div class="paginate-wrapper">

        <ul class="pagination">

            <li class="{{ (arrays.currentPage == 1) ? 'active' : '' }}">
                <a href="{{ uri.link }}{{ (uri.request_sent) ? '&' : '?' }}page=1">First</a>
            </li>

            {% for page_number in 1..(arrays.lastPage) %}

                {% set half_total_links = 7 / 2 | round %}
                {% set from = arrays.currentPage - half_total_links %}
                {% set to = arrays.currentPage + half_total_links %}

                {% if arrays.currentPage < half_total_links %}
                    {% set to = (half_total_links - arrays.currentPage) + to %}
                {% endif %}

                {% if (arrays.lastPage - arrays.currentPage) < half_total_links %}
                    {% set from = (half_total_links - (arrays.lastPage - arrays.currentPage) - 1) - to %}
                {% endif %}

                {% if from < page_number and page_number < to %}
                    <li class="{{ (arrays.currentPage == page_number) ? 'active' : '' }}">
                        <a href="{{ uri.link }}{{ (uri.request_sent) ? '&' : '?' }}page={{ page_number }}">{{ page_number }}</a>
                    </li>
                {% endif %}
            {% endfor %}

            <li class="{{ (arrays.currentPage == arrays.lastPage) ? 'active' : '' }}">
                <a href="{{ uri.link }}{{ (uri.request_sent) ? '&' : '?' }}page={{ arrays.lastPage }}">
                    Last
                </a>
            </li>
        </ul>
    </div>
{% endif %}

它的作用是,每次您搜索某个内容时,当您更改页面时,您发送的所有请求都不会消失。

在控制器中,您可以执行以下操作:

$routes = $this->route->paginate(5);
$window = Illuminate\Pagination\UrlWindow::make($routes);
$elements = [
    $window['first'],
    is_array($window['slider']) ? '...' : null,
    $window['slider'],
    is_array($window['last']) ? '...' : null,
    $window['last'],
];
// $this->twig is an instance of \Twig_Environment
// before render the template below, you should add `is_array` and `is_string` funcion to twig first
$this->twig->addFunction('is_string', new \Twig_SimpleFunction('is_string', function ($val) {
    return is_string($val);
}));
$this->twig->addFunction('is_array', new \Twig_SimpleFunction('is_array', function ($val) {
    return is_array($val);
}));
echo $this->twig->render(
        'pageNav.html',
        ['paginator' => $routes, 'elements' => array_filter($elements)]
    );
您的模板文件(pageNav.html)如下所示:

{% if paginator.hasPages %}
<ul class="am-pagination">
    {% if paginator.onFirstPage %}
    <li class="am-disabled"><span>&laquo;</span></li>
    {% else %}
    <li><a href="{{ paginator.previousPageUrl }}" rel="prev">&laquo;</a></li>
    {% endif %}

    {% for element in elements %}
        {% if is_string(element) %}
        <li class="am-disabled"><span>{{ element }}</span></li>
        {% endif %}

        {% if is_array(element) %}
            {% for page, url in element %}
            {% if paginator.currentPage == page %}
            <li class="am-active"><span>{{ page }}</span></li>
            {% else %}
            <li><a href="{{ url }}">{{ page }}</a></li>
            {% endif %}
            {% endfor %}
        {% endif %}
    {% endfor %}

    {% if paginator.hasMorePages %}
    <li><a href="{{ paginator.nextPageUrl }}" rel="next">&raquo;</a></li>
    {% else %}
    <li class="am-disabled"><span>&raquo;</span></li>
    {% endif %}
</ul>
{% endif %}
{%if paginator.hasPages%}
    {%if paginator.onFirstPage%}
  • « {%else%}
  • {%endif%} {elements%%中元素的百分比} {%if是_字符串(元素)%}
  • {{element}
  • {%endif%} {%if是_数组(元素)%} {对于页面,url位于元素%} {%if paginator.currentPage==page%}
  • {{page}
  • {%else%}
  • {%endif%} {%endfor%} {%endif%} {%endfor%} {%if paginator.hasMorePages%}
  • {%else%}
  • » {%endif%}
{%endif%}

祝您好运:)

在PHP7中,您可以创建匿名类来注册它,以便在viewVactory中使用:

$view = $this->container->get('view');

$view->getEnvironment()->addTest(new Twig_SimpleTest('string', function($value) {
    return is_string($value);
}));

\Illuminate\Pagination\Paginator::viewFactoryResolver(function() use ($view) {
    return new class($view) {
        private $view;
        private $template;
        private $data;

        public function __construct(\Slim\Views\Twig $view)
        {
            $this->view = $view;
        }

        public function make(string $template, $data = null)
        {
            $this->template = $template;
            $this->data = $data;
            return $this;
        }

        public function render()
        {
            return $this->view->fetch($this->template, $this->data);
        }
    };
});

\Illuminate\Pagination\Paginator::currentPageResolver(function() use ($request) {
    return $request->getParam('page');
});

return $view->render($response, 'list.twig', [
    'items' => Material::paginate(10),
]);
在<代码>列表.细枝中:

{{ items.render('pagination.twig') | raw }}
{% if paginator.hasPages() %}
    <ul class="pagination">
        {% for element in elements %}
            {% if element is string %}
                <li class="disabled"><span>{{ element }}</span></li>
            {% endif %}

            {% if element is iterable %}
                {% for page, url in element %}
                    {% if page == paginator.currentPage() %}
                        <li class="active"><span>{{ page }}</span></li>
                    {% else %}
                        <li><a href="{{ url }}">{{ page }}</a></li>
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% endfor %}
    </ul>
{% endif %}
分页.细枝中

{{ items.render('pagination.twig') | raw }}
{% if paginator.hasPages() %}
    <ul class="pagination">
        {% for element in elements %}
            {% if element is string %}
                <li class="disabled"><span>{{ element }}</span></li>
            {% endif %}

            {% if element is iterable %}
                {% for page, url in element %}
                    {% if page == paginator.currentPage() %}
                        <li class="active"><span>{{ page }}</span></li>
                    {% else %}
                        <li><a href="{{ url }}">{{ page }}</a></li>
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% endfor %}
    </ul>
{% endif %}
{%if paginator.hasPages()%}
    {elements%%中元素的百分比} {%如果元素是字符串%}
  • {{element}
  • {%endif%} {%if元素是iterable%} {对于页面,url位于元素%} {%if page==paginator.currentPage()%}
  • {{page}
  • {%else%}
  • {%endif%} {%endfor%} {%endif%} {%endfor%}
{%endif%}
我必须综合使用上面的答案才能让某些东西完全发挥作用。我将在这里发布我的解决方案,以防它对其他人有帮助

这是Wesley的模板,但已升级为使用Bootstrap4。作为奖励,它不需要中间件来设置任何全局变量

// pagination.twig
{% if data is not empty and data.lastPage > 1 %}

{# Pagination shows a "window" of links to the left and right of the current page #}
{% set num_visible_pages = 20 %}

<ul class="pagination">

  <li class="page-item {{ (data.currentPage == 1) ? 'disabled' : '' }}">
    <a class="page-link" href="{{ data.url(1) }}">First</a>
  </li>

  {% for page_number in 1..(data.lastPage) %}
    {% set half_visible_links = num_visible_pages / 2 | round %}
    {% set from = data.currentPage - half_visible_links %}
    {% set to = data.currentPage + half_visible_links %}

    {# if near beginning of pages, extend end to ensure num_visible_pages are shown #}
    {% if data.currentPage < half_visible_links %}
      {# we can be sloppy because the loop iteration constrains-out-of-bounds values #}
      {% set to = (half_visible_links - data.currentPage) + to %}
    {% endif %}

    {# if near end of pages, extend beginning to ensure num_visible_pages are shown #}
    {% if (data.lastPage - data.currentPage) < half_visible_links %}
      {# we can be sloppy because the loop iteration constrains-out-of-bounds values #}
      {% set from = data.lastPage - num_visible_pages %}
    {% endif %}

    {# only print pages between "from" and "to" #}
    {% if from < page_number and page_number < to %}
      <li class="page-item {{ (data.currentPage == page_number) ? 'active' : '' }}">
        <a class="page-link" href="{{ data.url(page_number) }}">{{ page_number }}</a>
      </li>
    {% endif %}
  {% endfor %}

  <li class="page-item {{ (data.currentPage == data.lastPage) ? 'disabled' : '' }}">
    <a class="page-link" href="{{ data.url(data.lastPage) }}">Last</a>
  </li>

</ul>

{% endif %}
然后,您需要告诉有口才的分页者如何从url获取“页面”(否则您将停留在第1页)。您可以在paginator()查询之前调用currentPageResolver()来完成此操作。对我来说,我只是把它放进了一些中间件中,所以它总是被定义的

<?php

/**
 * Tells the Eloquent Paginator how to get the current page
 */

namespace App\Middleware;

class Pagination {

    public function __invoke($request, $response, $next)
    {
        \Illuminate\Pagination\Paginator::currentPageResolver(function() use ($request) {
            return $request->getParam('page');
        });

        return $next($request, $response);
    }

}

您可能希望向url添加更多参数,例如排序或切换数据。您仍然可以使用
$items->appends()
执行此操作。请注意,默认路径是“/”。您可以使用
$items->withPath(“”)
删除它,这样用
->url()生成的链接将链接到当前页面。

有一个小问题:

{% if (data.lastPage - data.currentPage) < half_total_links %}
    {% set from = (half_total_links - (data.lastPage - data.currentPage) - 1) - to %}
{% endif %}

你找到5.3的解决方案了吗?这是可行的,但是大约有20页,最后一页没有正确计算“from”数,所以我用
{%set from=(half_total_links-(data.lastPage-data.currentPage)-1)-替换成%}
{%set from=(data.currentPage)-half_total_links-1%}
,谢谢Ignacio。不久前我遇到了一些bug,忘记了更新这个答案。当您接近大量页面的末尾时,也会出现问题。您的修正可能也适用于此问题的其他答案。