laravel 部署后分页出现问题,急求帮助。

2018-04-18 13:10:07 +08:00
 xuyl

项目在本地用 php artisan serve 运行正常,分页没问题。然后尝试用 docker 来部署,用的是这个镜像richarvey/nginx-php-fpm,其中 nginx 的配置如下:

server {
    listen 80;
    server_name example.com;
    root /example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

分页部分代码如下

    public function index()
    {
        $permissions = AdminPermission::paginate(20);
        return view('permission.index', compact('permissions'));
    }

模板里面也用到了 {{$permissions->links()}}

按文档来说,laravel 默认接收 page 参数作为分页依据,访问 /xxx?page=n 来翻页。然而发现翻页无效,返回的结果都是第一页的内容(可以保证不止一页),并且模板里的分页部分当前页也永远是第一页。有人遇到过吗? 另,除了分页有问题,其他所有都正常。

2370 次点击
所在节点    PHP
7 条回复
ahkxhyl
2018-04-18 13:29:50 +08:00
nginx 配置问题 这个问题我遇到过 2 次
ahkxhyl
2018-04-18 13:30:39 +08:00
检查下 try_files 那段
xuyl
2018-04-18 14:16:25 +08:00
@ahkxhyl 按我的理解,通过 try_files 来判断请求是否是静态资源(实际存在的资源),是则返回文件,不是便将请求转发到 /index.php 下,并带上 query_string 的内容。
justfindu
2018-04-18 14:19:46 +08:00
pathinfo
ahkxhyl
2018-04-18 14:32:01 +08:00
@xuyl try_files 那段没问题 我以前遇到的问题就是这块。分页始终是第一页~~
xuyl
2018-04-18 14:55:37 +08:00
搞定了,多谢 @ahkxhyl,是我粗心了,query_string 忘了$
ahkxhyl
2018-04-18 15:25:21 +08:00
@xuyl 我看你贴出来的 query_string 带有? 所以我也很奇怪,。。如果没有?,分页不正常 问题就是这里

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/447780

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX