【问题标题】:pagination and factory in laravel4laravel4 中的分页和工厂
【发布时间】:2015-06-14 15:04:10
【问题描述】:

考虑一下这个取自here的代码。

 public function getIndex() 
 {
     $posts = Post::orderBy('id','desc')->paginate(10);
     // For Laravel 4.2 use getFactory() instead of getEnvironment() method.
     $posts->getEnvironment()->setViewName('pagination::simple');
     $this->layout->title = 'Home Page | Laravel 4 Blog';
     $this->layout->main = View::make('home')->nest('content','index',compact('posts'));
 }

据我了解,分页限制了行数,所以我认为 paginate(10) 意味着选择数据库中的前十行。但我绝对不明白。

     // For Laravel 4.2 use getFactory() instead of getEnvironment() method.
     $posts->getEnvironment()->setViewName('pagination::simple');

     $posts->getFactory()->setViewName('pagination::simple');

以及下面的所有内容。主要是我不明白工厂是什么意思以及它与分页的关系。我去了Illuminate\Pagination\FactoryIlluminate\View\View 上的laravel 文档,但我找不到工厂的含义。谁能解释一下上面的代码?

【问题讨论】:

  • 我认为它设置了使用的分页类型,如果您在 laravel 文档中找不到某些内容,您将需要直接检查代码库,这可以在 github 或项目

标签: php laravel-4


【解决方案1】:

您实际上是通过选择特定的分页器视图来设置分页在 HTML 中的输出方式,这允许您在应用程序中拥有多个类型或使用与默认值不同的类型。

在同一个应用程序中使用多种分页类型

有时,您可能希望在整个页面中使用不同的分页类型 应用。默认情况下,Laravel 会使用你指定的类型 app/config/view.php 文件,所以你需要覆盖这个设置 您希望使用其他类型。以下是如何执行此操作。

// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.

$articles = Article::paginate(5);

Paginator::setViewName('pagination::simple');

/*
Alternatively, you could also use this to achieve the same result:

$articles->getEnvironment()->setViewName('pagination::simple');

For those who would like to know what’s happening under the hood, here is a more
detailed explanation:

1. Calling paginate() on an Eloquent model or a query builder will return an
   instance of \Illuminate\Pagination\Paginator

2. Then, we need to get the related \Illuminate\Pagination\Environment of this
   paginator via the well-named getEnvironment() method.

3. Finally, we can specify the pagination type we need. The default value is
   'pagination::slider'.

The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/

来源:http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application

【讨论】:

  • 你能解释一下 getEnvironment() 和 getFactory() 函数是什么吗?他们是做什么的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2022-01-26
相关资源
最近更新 更多