Laravel 5 - 漂亮的分页师

时间:2014-12-29 10:54:38

标签: php laravel pagination laravel-5

所以我试图在 Laravel 5 中使用localhost/ads/1这样的漂亮网址获取分页,其中1代表页面。

据我了解,此类操作需要重载AbstractPaginatorLengthAwarePaginator,以便对Database\Query\Builder进行修改。

我是否遗漏了某些东西,绑定或依赖注入,或者是否有可能更改我们想要使用的分页器?

3 个答案:

答案 0 :(得分:3)

最后,我不得不自己编写一个Paginator。我在这里发布我的解决方案,它是否对任何人都有帮助。

请注意,该解决方案虽然功能齐全,但需要注意实际使用(关于验证);这里简化了类,以突出机制。

<?php namespace App\Services;

use Illuminate\Support\Collection;
use Illuminate\Pagination\BootstrapThreePresenter;
use Illuminate\Pagination\LengthAwarePaginator as BasePaginator;

class Paginator extends BasePaginator{



     /**
     * Create a new paginator instance.
     *
     * @param  mixed  $items
     * @param  int  $perPage
     * @param  string $path Base path
     * @param  int $page
     * @return void
     */
    public function __construct($items, $perPage, $path, $page){
        // Set the "real" items that will appear here
        $trueItems = [];

        // That is, add the correct items
        for ( $i = $perPage*($page-1) ; $i < min(count($items),$perPage*$page) ; $i++ ){
            $trueItems[] = $items[$i];
        }

        // Set path as provided
        $this->path = $path;

        // Call parent
        parent::__construct($trueItems,count($items),$perPage);

        // Override "guessing" of page
        $this->currentPage = $page;
    }

    /**
     * Get a URL for a given page number.
     *
     * @param  int  $page
     * @return string
     */
    public function url($page){
        if ($page <= 0) $page = 1;

        return $this->path.$page;
    }
}

要使用该类,您可以定义路径

Route::get('items/{page}','MyController@getElements');

然后在所述控制器中,getElements

$items = new Paginator(Model::all(),$numberElementsPerPage,url('items'),$page);

然后,您可以像往常一样处理元素。 注意:我添加了一个路径选项,以便集成更复杂的漂亮网址设计。 希望它有所帮助!

答案 1 :(得分:1)

我为laravel 5.3编写了这个没有性能负面的东西: PlumPrettyUrlPaginator.php

<?php
namespace Plum\Cmc\Paginator;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class PlumPrettyUrlPaginator extends LengthAwarePaginator{

  /**
   * basically a copy of LengthAwarePaginator constructor and then replacing all in our own
   */
  public function __construct(LengthAwarePaginator $p, $path, $currentPage = null) {

    $this->total = $p->total;
    $this->perPage = $p->perPage;
    $this->lastPage = (int) ceil($p->total / $p->perPage);
//    $this->path = ((stripos(strrev($p->path), '/') === 0) ? $p->path : $p->path.'/');
    $this->path = $path;
    $this->currentPage = $p->setCurrentPage($currentPage ?? $p->currentPage, $p->pageName);
    $this->items = $p->items;
  }

  public function url($page){
    if ($page <= 0) $page = 1;
    return $this->path.$page;
  }
}

路线:

Route::get('/', [ function (Request $request) {
  $data = PlumSearchService::PrepareSearchView($request);
  return view('inventory.search_products', $data);
}]);


Route::get('/page/{pageNr}', [ function (Request $request, int $pageNr) {
  $data = PlumSearchService::PrepareSearchView($request, false, $pageNr);
  return view('inventory.search_products', $data);
}]);

然后在PlumSearchService

$query = Product::select('product.*') //...
       $paginator = $query->paginate(15, ['*'], 'page', $pageNr);
        $products = new PlumPrettyUrlPaginator($paginator, '/page/', $pageNr);

答案 2 :(得分:-4)

.htaccess 中删除此RewriteRule ^(.*)/$ /$1 [L,R=301]代码行。