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 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误