2014-12-29 3 views
5

그래서 페이지 매김을 얻으려고합니다. Laravel 5localhost/ads/1과 같은 예쁜 URL이 있습니다. 여기에서 1은 페이지를 나타냅니다.Laravel 5 - 귀여운 페이지 매김

필자의 이해에 따라, 이러한 작업은 의 수정을 목표로 AbstractPaginator 또는 LengthAwarePaginator의 오버로드가 필요합니다.

나는 바인딩이나 의존성 삽입이 누락되었거나 사용하려는 페이징을 변경할 가능성이 있습니까?

+0

에서 다음

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

을 정확한 쿼리 문자열에 대한 URL? 내부적으로'localhost/ads? page = 1'로 변환하십시오. –

+0

@BenHarold 물론, 링크를 리디렉션 할 수 있습니다. 그러나 링크 생성 문제는 변경되지 않습니다. – repptilia

답변

3

결국 나는 Paginator를 코딩해야했습니다. 내가 여기에 내 솔루션을 올리면 누군가에게 도움이 될 것입니다.

솔루션은 기능적이지만 실제 사용 (검증)에 대한주의가 필요합니다. 클래스는 메커니즘을 강조하기 위해 여기에서 단순화되었습니다. 그런 다음

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

, 당신은 귀하의 요소를 처리 할 수 ​​있습니다

<?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}','[email protected]'); 

, getElements의를 정의 할 수있는 클래스를 사용하려면 일반적으로. 참고 : 더 복잡한 pretty url 디자인을 통합하기 위해 경로 옵션을 추가했습니다. 도움이 되었기를 바랍니다.

+1

당신이 원하는 페이지 매김을하기 전에 전체 테이블을 메모리에로드해야한다는 것을 의미하지는 않습니까? – Puzbie

+0

'Model :: all'은 전체 세트를 반환합니다 ... 잔인한가요? – Toskan

-3

RewriteRule ^(.*)/$ /$1 [L,R=301] 코드 줄을 .htaccess에서 제거하십시오.

+1

그 줄은 질문과 아무런 관련이 없습니다. – ceejayoz

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; 
    } 
} 

경로 : 당신이 변환`mod_rewrite`를 사용할 수 PlumSearchService

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