Laravel Pagination with pretty urls than query string

拜拜、爱过 提交于 2019-12-23 03:32:44

问题


with credit to the user called Kindari @ irc room #laravel in freenode , also credit goes to user iampseudo and Debolaz.

with following laravel route code,

Route::bind('key_pairs', function($s) {
// some logic to transform string to associative array
$arr = explode("/",$s);
$arr2 = array();
if(count($arr)%2 == 0)
{
    for($i=0;$i<count($arr);$i+=2)
    {
        $arr2[$arr[$i]] = $arr[$i+1];
    }
}
return $arr2;   
});

Route::get('foo/{key_pairs}', function($key_pairs) {
var_dump($key_pairs);
})->where('key_pairs', '.*'); 

now we can get /foo/page/1 for Laravel to read as /foo?page=1 , but former is more prettier than latter.

now what is needed here is for Laravel's pagination instance to read /page/1 rather than ?page=1 , so pretty pagination urls will work smoothly.

Does anyone know now to do this , without altering the base code ?

if we can have something like Users::paginate(5)->page($page) or any other functionality if already exists (which i am unable to find) , that is great.

cheers


回答1:


Okay problem solved , now pretty pagination urls for Laravel is working and here is the solution.

i added the getByPage method into the relevant model class posted in the following link (Credit goes to him)

and called $this->user->getByPage($page, $limit); in the Routes ,

There we have Pretty pagination URLs !



来源:https://stackoverflow.com/questions/22124593/laravel-pagination-with-pretty-urls-than-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!