Laravel pagination pretty URL

2022-08-30 18:34:39

有没有办法在Laravel 4中获得一个分页漂亮的URL?

例如,默认情况下:

http://example.com/something/?page=3

我想得到的:

http://example.com/something/page/3

此外,分页应以这种方式呈现,并且追加到分页应以这种方式显示。


答案 1

这是一个黑客解决方法。我正在使用Laravel v4.1.23。它假设页码是 URL 的最后一位。没有深入测试过,所以我对人们能找到的任何错误都感兴趣。我对更好的解决方案更感兴趣:-)

路线:

Route::get('/articles/page/{page_number?}', function($page_number=1){
    $per_page = 1;
    Articles::resolveConnection()->getPaginator()->setCurrentPage($page_number);
    $articles = Articles::orderBy('created_at', 'desc')->paginate($per_page);
    return View::make('pages/articles')->with('articles', $articles);
});

视图:

<?php
    $links = $articles->links();
    $patterns = array();
    $patterns[] = '/'.$articles->getCurrentPage().'\?page=/';
    $replacements = array();
    $replacements[] = '';
    echo preg_replace($patterns, $replacements, $links);
?>

型:

<?php
class Articles extends Eloquent {
    protected $table = 'articles';
}

迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration {

    public function up()
    {
        Schema::create('articles', function($table){
            $table->increments('id');
            $table->string('slug');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('articles');
    }
}

答案 2

这是可能的,但你需要编码一点。

首先,您需要更改分页服务提供者 - 您需要编写自己的分页服务提供者。app/config/app.php

评论:

// 'Illuminate\Pagination\PaginationServiceProvider',

并添加

'Providers\PaginationServiceProvider',

在提供者部分中。

现在,您需要创建分页服务提供程序才能使用自定义分页工厂:

model/Providers/PaginationServiceProvider.php文件:

<?php

namespace Providers;

use Illuminate\Support\ServiceProvider;

class PaginationServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bindShared('paginator', function ($app) {
            $paginator = new PaginationFactory($app['request'], $app['view'],
                $app['translator']);

            $paginator->setViewName($app['config']['view.pagination']);

            $app->refresh('request', $paginator, 'setRequest');

            return $paginator;
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('paginator');
    }

}

上面你创建对象,所以现在我们需要创建这个文件:Providers\PaginationFactory

model/providers/PaginationFactory.php文件:

<?php


namespace Providers;

use Illuminate\Pagination\Factory;


class PaginationFactory extends  Factory {

    /**
     * Get a new paginator instance.
     *
     * @param  array  $items
     * @param  int    $total
     * @param  int|null  $perPage
     * @return \Illuminate\Pagination\Paginator
     */
    public function make(array $items, $total, $perPage = null)
    {
        $paginator = new \Utils\Paginator($this, $items, $total, $perPage);

        return $paginator->setupPaginationContext();
    }        
} 

在这里,您只创建对象,所以现在让我们创建它:\Utils\Paginator

model/Utils/Paginator.php文件:

<?php

namespace Utils;



class Paginator extends \Illuminate\Pagination\Paginator {


    /**
     * Get a URL for a given page number.
     *
     * @param  int  $page
     * @return string
     */
    public function getUrl($page)
    {
      $routeParameters = array();

      if ($page > 1) { // if $page == 1 don't add it to url
         $routeParameters[$this->factory->getPageName()] = $page;
      }

      return \URL::route($this->factory->getCurrentUrl(), $routeParameters);
    }
}

在此文件中,我们最终覆盖了用于创建分页 URL 的默认方法。

假设您以这种方式定义了路由:

Route::get('/categories/{page?}',
    ['as'   => 'categories',
     'uses' => 'CategoryController@displayList'
    ])->where('page', '[1-9]+[0-9]*');

正如你所看到的,我们在这里定义了路由名称(这很重要,因为上面的分页器实现 - 但你当然可以用不同的方式做到这一点)。as

现在在类的方法中,你可以做:displayListCategoryController

public function displayList($categories, $page = 1) // default 1 is needed here 
{
    Paginator::setCurrentPage($page);
    Paginator::setBaseUrl('categories'); // use here route name and not the url
    Paginator::setPageName('page');

    $categories = Category::paginate(15);


    return View::make('admin.category')->with(
        ['categories' => $categories]
    );
}

在视图中添加:

<?php echo $categories->links(); ?>

您将通过以下方式获得生成的网址:

http://localhost/categories
http://localhost/categories/2
http://localhost/categories/3
http://localhost/categories/4
http://localhost/categories/5

没有?在查询字符串中

但是,在我看来,默认情况下应该添加这样的东西,或者至少应该足以扩展一个类,而不是仅仅为了实现一个方法而创建3个类。


推荐