douzhang1299 2014-08-20 13: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 14: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.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部