V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xuyl
V2EX  ›  PHP

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

  •  
  •   xuyl · 2018-04-18 13:10:07 +08:00 · 2364 次点击
    这是一个创建于 2192 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目在本地用 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 来翻页。然而发现翻页无效,返回的结果都是第一页的内容(可以保证不止一页),并且模板里的分页部分当前页也永远是第一页。有人遇到过吗? 另,除了分页有问题,其他所有都正常。

    7 条回复    2018-04-18 15:25:21 +08:00
    ahkxhyl
        1
    ahkxhyl  
       2018-04-18 13:29:50 +08:00 via Android
    nginx 配置问题 这个问题我遇到过 2 次
    ahkxhyl
        2
    ahkxhyl  
       2018-04-18 13:30:39 +08:00 via Android
    检查下 try_files 那段
    xuyl
        3
    xuyl  
    OP
       2018-04-18 14:16:25 +08:00
    @ahkxhyl 按我的理解,通过 try_files 来判断请求是否是静态资源(实际存在的资源),是则返回文件,不是便将请求转发到 /index.php 下,并带上 query_string 的内容。
    justfindu
        4
    justfindu  
       2018-04-18 14:19:46 +08:00
    pathinfo
    ahkxhyl
        5
    ahkxhyl  
       2018-04-18 14:32:01 +08:00
    @xuyl try_files 那段没问题 我以前遇到的问题就是这块。分页始终是第一页~~
    xuyl
        6
    xuyl  
    OP
       2018-04-18 14:55:37 +08:00
    搞定了,多谢 @ahkxhyl,是我粗心了,query_string 忘了$
    ahkxhyl
        7
    ahkxhyl  
       2018-04-18 15:25:21 +08:00
    @xuyl 我看你贴出来的 query_string 带有? 所以我也很奇怪,。。如果没有?,分页不正常 问题就是这里
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5192 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:29 · PVG 17:29 · LAX 02:29 · JFK 05:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.