Laravel pagination pretty URL

后端 未结 6 1082
孤街浪徒
孤街浪徒 2020-11-27 19:35

Is there a way to get a pagination pretty URL in Laravel 4?

For example, by default:

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

And what

6条回答
  •  臣服心动
    2020-11-27 20:20

    Here's a hacky workaround. I am using Laravel v4.1.23. It assumes page number is the last bit of your url. Haven't tested it deeply so I'm interested in any bugs people can find. I'm even more interested in a better solution :-)

    Route:

    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);
    });
    

    View:

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

    Model:

    Migration:

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

提交回复
热议问题