douzhang1299
2014-08-20 21:48
浏览 61
已采纳

Laravel 4 - 在一个页面上使用多个分页器

I've been googling for this question but can't find any good answers. I found that there is a method called setPageName() for the paginator but it only works when used like Paginator::setPageName('page').

I looked up the paginator in the Laravel API (http://laravel.com/api/class-Illuminate.Pagination.Environment.html) and the method is a public function just like setBaseUrl which I am currently using like $albums->setBaseUrl('/album/');

for instance I want to have a Photo paginator (?photoPage=n) and a Comment paginator (?commentPage=n).

Is there any way that I can use two paginators that use different page names in the URL on one page, or at least call the setPageName() method like setBaseUrl()?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtamho6404 2014-08-20 22:17
    已采纳

    There are actually two classes at play here:

    Where the Factory class actually orchestrates the creation of Paginator instances. After a quick glance, I couldn't really tell you why the Factory class is in charge of get/setPageName(), but that's where they live.

    It seems that you would need two distinct Factory instances, which, fortunately, is possible via the IoC Container. Since the PaginationServiceProvider binds to paginator, calling $app->make("Illuminate\Pagination\Factory") twice should generate two distinct factories. If you're using namespaces, a service provider to inject the factories into your controller might look like:

    class DoublyPaginatedControllerServiceProvider extends ServiceProvider {
        public function register($app)
        {
            $this->bindShared('paginator.photos', function ($app) {
                $factory = $app->make('Illuminate\Pagination\Factory');
                $factory->setPageName('photoPage');
                return $factory;
            });
    
            $this->bindShared('paginator.comments', function ($app) {
                $factory = $app->make('Illuminate\Pagination\Factory');
                $factory->setPageName('commentPage');
                return $factory;
            });
    
            $this->app->bindShared('Namespace\DoublyPaginatedController', function ($app) {
                return new DoublyPaginatedController(
                    $app['paginator.photos'],
                    $app['paginator.comments']
                );
            }
        }
    }
    

    Of course, the downside here is that you can't use Eloquent's Model::paginate() -- you'd need to manually determine which set of items to get and pass them to each Factory's make() method.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题