Eloquent paginate function in Slim 3 project using twig

十年热恋 提交于 2019-12-24 00:20:13

问题


How can I use paginate function from Eloquent in Slim 3 project using twig ?

This is in my controller :

$posts = Sound::paginate(2);

$this->container->view->render($response, 'admin/sounds/index.twig', [
  'posts' => $posts
]);

This is the view :

{{ posts.links() }}

But it doesn't work as well as I expected :

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in **PATH_TO_PROJECT**\vendor\illuminate\pagination\AbstractPaginator.php on line 412

Fatal error: Call to a member function make() on null in **PATH_TO_PROJECT**\vendor\illuminate\pagination\LengthAwarePaginator.php on line 90

What I have to do to make it work ?


回答1:


Can you try this:

{{ posts.links }}

I presume that links is a getter that returns links. If not, this won't work like you expect.




回答2:


First, you need to include illuminate/pagination in your project (it's not included with illuminate/database):

composer require illuminate/pagination

Now paginator needs to know how to resolve current page. You should make sure this is done before using paginator, I personally put it where I'm setting up dependencies:

// $container is application's DIC container.
// Setup Paginator resolvers                                                                                                                                                                                       
Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') use ($container) {                                                                                                              

    $page = $container->request->getParam($pageName);                                                                                                                                                              

    if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {                                                                                                                                    
        return $page;                                                                                                                                                                                              
    }                                                                                                                                                                                                              
    return 1;                                                                                                                                                                                                      
});

Then in your twig template you can output pagination links. But please you should notice that paginator generates some HTML code which needs to be written to output as is so you'll need to tell twig to ignore escaping for links:

{{ posts.links | raw }}



回答3:


Sorry for the late :

I didn't keep the project, I don't remember exactly how I did, but this : https://github.com/romanzipp/PHP-Slim-Pagination looks like what I did.

$app->get('/posts', function(Request $req,  Response $res, $args = []) use ($cache) {

   $page      = ($req->getParam('page', 0) > 0) ? $req->getParam('page') : 1;
   $limit     = 5; // Number of posts on one page
   $skip      = ($page - 1) * $limit;
   $count     = Post::getCount([]); // Count of all available posts

   return $this->view->render($res, 'post-list.twig', [
      'pagination'    => [
          'needed'        => $count > $limit,
          'count'         => $count,
          'page'          => $page,
          'lastpage'      => (ceil($count / $limit) == 0 ? 1 : ceil($count / $limit)),
          'limit'         => $limit,
       ],
       // return list of Posts with Limit and Skip arguments
       'posts'         => Post::getList([
          'limit'         => $limit,
          'skip'          => $skip,
       ])
   ]);
});

In template :

 {% if pagination.needed %}
    <div class="ui pagination menu">
        {% for i in 1..pagination.lastpage %}
           <a class="{% if i == pagination.page %}active{% endif %} item" href="?page={{ i }}">{{ i }}</a>
       {% endfor %}
    </div>
{% endif %}

<div class="ui container">
    {% for post in posts %}
       <a class="item">
            {# Post contents (title, url, ...) #}
       </a>
   {% endfor %}
</div>


来源:https://stackoverflow.com/questions/44832193/eloquent-paginate-function-in-slim-3-project-using-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!