2017-06-29 51 views
2

如何在使用樹枝的Slim 3項目中使用Eloquent中的Paginate功能?Slim 3項目中使用樹枝的雄辯分頁功能

這是在我的控制器:

$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 

我必須做些什麼來使其工作?

回答

-1

對不起已故:

我沒有保留這個項目,我不記得我是怎麼做的,但是這個: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> 
+0

雖然這可能會在理論上回答這個問題,但[這將是更可取的](// meta.stackoverflow.com/q/8259)在這裏包含答案的基本部分,並提供供參考的鏈接。 – GhostCat

0

你可以試試這個:

{{ posts.links }} 

我猜想links是返回鏈接吸氣。如果沒有,這不會像你期望的那樣工作。

0

首先,你需要包括照射/分頁項目中的(它不包括照射/數據庫):

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代碼:

{{ posts.links | raw }} 
+0

嗨,好知道,但我做了我自己PAGINATE系統。我沒有嘗試你的解決方案。 – Swarovski

+1

發佈您的解決方案將很好,所以其他人會知道有其他方法來實現這一點。 – Nima