laravel 5 中的分页产生错误链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29835026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:25:55  来源:igfitidea点击:

Pagination in laravel 5 producing wrong link

laravellaravel-5

提问by Ayman Hussein

When creating pagination on users page the pagination links are not right they are with / before page=1 so the link redirect to root with 404 not found.

在用户页面上创建分页时,分页链接不正确,它们在 page=1 之前使用 / ,因此链接重定向到根目录,但找不到 404。

Controller method:

控制器方法:

public function getRegister()
{
    $users = User::where("admin", 0)->paginate(3);

    return view('auth.register', compact('users'));
} 

view

看法

<?php echo $users->render(); ?>

users page url: http://localhost:8080/pal/public/agent/create

用户页面网址:http://localhost:8080/pal/public/agent/create

pagination links:http://localhost:8080/pal/public/agent/create/?page=1

分页链接:http://localhost:8080/pal/public/agent/create/?page=1

when click on paging link browser redirect me to: http://localhost:8080/agent/create?page=3and got 404 page NOT FOUND

当点击分页链接浏览器将我重定向到:http://localhost:8080/agent/create?page=3并得到 404 page NOT FOUND

回答by Carter

Unfortunately, that's, at least for now, how Laravel 5 works.

不幸的是,至少就目前而言,这就是 Laravel 5 的工作方式。

To get rid of that additional slash, you have to manually handle pagination links before rendering them.

为了摆脱那个额外的斜线,你必须在渲染它们之前手动处理分页链接。

You can either: 1) in your controller, call setPath() on the paginator instance, like this:

您可以: 1) 在您的控制器中,在分页器实例上调用 setPath(),如下所示:

public function getRegister()
{
    $users = User::where('admin',0)->paginate(3);
    $users->setPath('your/custom/path');
    return view('your/view')->with('users',$users);
}

Next in your view, simply call render() and it should work as expected.

接下来在您的视图中,只需调用 render() ,它就会按预期工作。

or 2) modify nothing in controller but change link format in your view, for example:

或 2) 在控制器中不做任何修改,但在您的视图中更改链接格式,例如:

{!! str_replace('/?', '?', $users->render()) !!}

You may refer to the questionI raised before.

你可以参考我之前提出的问题

Hope this helps!

希望这可以帮助!



I am still looking for more elegant solution but have not got any yet :(

我仍在寻找更优雅的解决方案,但还没有任何解决方案:(



Updated on 2016-05-30

更新于 2016-05-30

Thanks for all the up-votes. Please note that in Laravel 5.2, you don't have to hack the code in a way mentioned above any more! Calling render() in views is all that you need to do. That's great, isn't it?

感谢所有的投票。请注意,在 Laravel 5.2 中,您不必再以上述方式破解代码了!您只需在视图中调用 render() 即可。那太好了,不是吗?

回答by Wreeecks

I like carter's answer, I prefer the first suggestion which is using setPath. Setting "setPath" to an empty string worked for me. :)

我喜欢卡特的回答,我更喜欢使用 setPath 的第一个建议。将“setPath”设置为空字符串对我有用。:)

$users->setPath('');

回答by janhrubes

The best method here (without need to change the code) is to modify the .htacces file to:

这里最好的方法(无需更改代码)是将 .htacces 文件修改为:

RewriteEngine On
RewriteBase /pal/public/

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$  [L,R=301] <-- removed "/" before 

This way (without the note in this snippet) the links from pagination should be rendered correctly.

这样(没有此代码段中的注释)分页中的链接应该正确呈现。

回答by Maskipaps

Just to add on carters advised:

只是添加卡特斯建议:

    public function getRegister()
    {
       $users = User::where('admin',0)->paginate(3);
       $users->setPath('your/custom/path');
       return view('your/view')->with('users',$users);
    }

Add the 'url()' and concat it with the route. e.g '/admin/user/list'

添加 'url()' 并将其与路由连接。例如'/admin/user/list'

    $users->setPath(url() . '<route>');