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.

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加