【问题标题】:Eloquent paginate function in Slim 3 project using twigSlim 3项目中使用twig的雄辩分页功能
【发布时间】:2017-06-29 18:08:37
【问题描述】:

如何在 Slim 3 项目中使用 Eloquent 的分页功能使用 twig ?

这是在我的控制器中:

$posts = Sound::paginate(2);

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

这是视图:

{{ posts.links() }}

但效果不如我预期:

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

我必须做些什么才能让它工作?

【问题讨论】:

    标签: eloquent twig pagination slim-3


    【解决方案1】:

    你可以试试这个:

    {{ posts.links }}
    

    我认为links 是一个返回链接的getter。如果没有,这将不会像您期望的那样工作。

    【讨论】:

      【解决方案2】:

      首先,您需要在项目中包含照明/分页(照明/数据库中不包含):

      composer require illuminate/pagination
      

      现在分页器需要知道如何解析当前页面。您应该确保在使用分页器之前完成此操作,我个人将其放在我设置依赖项的位置:

      // $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;                                                                                                                                                                                                      
      });
      

      然后在您的树枝模板中,您可以输出分页链接。但是请您注意,分页器会生成一些 HTML 代码,这些代码需要按原样编写以输出 ,因此您需要告诉 twig 忽略链接的转义:

      {{ posts.links | raw }}
      

      【讨论】:

      • 嗨,很高兴知道,但我创建了自己的分页系统。我没有尝试您的解决方案。
      • 很高兴发布您的解决方案,这样其他人就会知道还有其他方法可以实现这一目标。
      • 你能详细介绍一下 Container 吗?
      • 你在哪里添加了这段代码?我正在使用他苗条的骨架并将其添加到依赖项中,但它并没有太大变化。
      • depwndencies.php 是它的好地方。您的代码的确切问题是什么?
      【解决方案3】:

      抱歉来晚了:

      我没有保留这个项目,我不记得我是怎么做的,但是这个:https://github.com/romanzipp/PHP-Slim-Pagination 看起来就像我做的那样。

      $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,
             ])
         ]);
      });
      

      在模板中:

       {% 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>
      

      【讨论】:

      • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2017-07-21
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多