Php 拉威尔5号-漂亮的翻页机

Php 拉威尔5号-漂亮的翻页机,php,laravel,pagination,laravel-5,Php,Laravel,Pagination,Laravel 5,因此,我试图在laravel5中使用类似localhost/ads/1的漂亮URL获得分页,其中1代表页面 据我所知,这样的操作需要重载AbstractPaginator,或Lengshawarepaginator,以修改数据库\Query\Builder 我是否遗漏了一些东西,绑定或依赖项注入,或者是否有可能更改我们想要使用的分页器?最后,我不得不为自己编写一个分页器。我在这里发布我的解决方案,如果它对任何人都有帮助的话 请注意,解决方案虽然具有功能性,但在实际使用中需要谨慎(关于验证);在这

因此,我试图在laravel5中使用类似
localhost/ads/1
的漂亮URL获得分页,其中
1
代表页面

据我所知,这样的操作需要重载
AbstractPaginator
,或
Lengshawarepaginator
,以修改
数据库\Query\Builder


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

最后,我不得不为自己编写一个分页器。我在这里发布我的解决方案,如果它对任何人都有帮助的话

请注意,解决方案虽然具有功能性,但在实际使用中需要谨慎(关于验证);在这里对该类进行了简化,以突出显示该机制

<?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;
    }
}
然后在所述控制器中,在
getElements
中:

$items = new Paginator(Model::all(),$numberElementsPerPage,url('items'),$page);
然后,您可以像平常一样处理元素。 注意:我添加了一个路径选项,以便集成更复杂的漂亮url设计。
希望有帮助

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

我为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;
  }
}
然后在
PlumSearchService

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

能否使用
mod_rewrite
将URL转换为正确的查询字符串?在内部将其转换为
localhost/ads?page=1
@BenHarold当然,这样可以重定向链接。但是,我认为这不会改变链接生成的问题。这一行与问题无关。这难道不意味着你必须先将整个表加载到内存中,然后才能对所需内容进行分页吗?
Model::all
返回整个集合。。。那太过分了,不是吗?
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);
}]);
$query = Product::select('product.*') //...
       $paginator = $query->paginate(15, ['*'], 'page', $pageNr);
        $products = new PlumPrettyUrlPaginator($paginator, '/page/', $pageNr);