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

关于 laravel 的路由解析问题

  •  
  •   waibunleung · 2017-12-29 13:53:58 +08:00 · 3849 次点击
    这是一个创建于 2300 天前的主题,其中的信息可能已经有所发展或是发生改变。
    情况是这样的,刚安装了 laravel,然后用 nginx 配置了 url 的重写
    #location / {
    # index index.php index.html index.htm;
    # try_files $uri $uri/ /index.php?$query_string;
    #}
    然后配置了 laravel 的路由:
    Route::get('/ex/am',function(){
    return 'hello laravel';
    });
    此时输入 http://localhost/ex/am 是显示 hello laravel 的
    然后我把 nginx 的重写注释掉,在浏览器输入:
    http://localhost/index.php/ex/am 显示 not found
    http://localhost/inde.php?/ex/am 也是 not found
    重新开启 nginx 重写后又能正常访问

    所以,我想知道为什么不开启重写,直接访问以上两个地址之一没法正常显示 hello laravel ??
    试过看源码,有点绕。。。所以来这边问一下
    11 条回复    2018-01-03 17:25:12 +08:00
    sunflyer
        1
    sunflyer  
       2017-12-29 14:16:13 +08:00
    /ex/am =》这部分叫 uri
    /ex/am?a=123&b=456 =>问号后面的叫 query_string
    emm,剩下的应该不用说了
    printempw
        2
    printempw  
       2017-12-29 14:33:24 +08:00 via Android
    你的意思是开启 Nginx URL 重写后 /index.php/ex/am 可以正常访问吗?你的请求确定经过 Laravel 了吗?提示的 Not found 是 Laravel 报的错还是 Nginx 报的错?

    稍微看了一下源码(人在外面),Laravel 从 REQUEST_URI 后似乎没有对 index.php 这类 script name 做特殊处理。所以你访问的 /index.php/ex/am 与你定义的路由 /ex/am 应该是**不匹配**的(没验证过,不一定对,手机看源码太痛苦了)

    https://github.com/laravel/framework/blob/5.5/src/Illuminate/Routing/Matching/UriValidator.php
    my101du
        3
    my101du  
       2017-12-29 14:39:26 +08:00 via Android
    未开启重写,末尾加斜杠。会被 nginx 去找匹配的子目录吧。
    kismetX
        4
    kismetX  
       2017-12-29 14:39:52 +08:00
    你的 nginx 配置有问题吧,就单 http://localhost/能访问吗?
    waibunleung
        5
    waibunleung  
    OP
       2017-12-30 15:21:38 +08:00
    @my101du 我没有末尾加斜杠啊
    waibunleung
        6
    waibunleung  
    OP
       2017-12-30 15:24:03 +08:00
    @kismetX 关于 网址是 localhost 只是用于方便提问的网址书写,我自己配的 nginx 本地网址是“ w w w .lab.com ”,请不要纠结 localhost 的问题
    waibunleung
        7
    waibunleung  
    OP
       2017-12-30 16:29:53 +08:00
    @printempw 你这么一说,是 nginx 报的 404 错误,请求应该没走 laravel,可是为什么 http://localhost/index.php?/ex/am 或者 http://localhost/index.php?/ex/am
    这样的请求没有走 laravel 的路由?已经进入入口文件了,路由却没有解析到

    另外,根据以上 nginx 的配置,这个 url: http://localhost/ex/am 重写之后真实访问的 url 是怎样的?
    waibunleung
        8
    waibunleung  
    OP
       2017-12-30 16:30:51 +08:00
    @my101du 我的意思是我没有开启重写,就不会匹配到斜杠那一项吧?
    waibunleung
        9
    waibunleung  
    OP
       2017-12-30 16:44:33 +08:00
    @printempw 开启 Nginx URL 重写后 /index.php/ex/am 确实可以正常访问
    medz
        10
    medz  
       2018-01-02 17:58:45 +08:00
    @waibunleung 很简单,Laravel 现在是采用 pathinfo 模式的,也就是通过请求路径进行分析路由,你可以开启重写后在 index.php 打印 $_SERVER 你再关闭后打印,你看 `REQUEST_URI` 部分是不一样的。
    waibunleung
        11
    waibunleung  
    OP
       2018-01-03 17:25:12 +08:00
    @medz
    重写前后,我看到的 REQUEST_URI 是一样的。。。。不知道你有没有试验过
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3446 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:59 · PVG 19:59 · LAX 04:59 · JFK 07:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.