Laravel路由奇怪的行为 [英] Laravel route strange behavior

查看:156
本文介绍了Laravel路由奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

Route::get('test/{n}', function ($n) {
    return $n;
});

App::missing(function () {
    return Response::make("Dude, that's a 404 !", 404);
});

适用于类似

http://localhost/myproject/public/test/something

但是

http://localhost/myproject/public/test/

重定向到

http://localhost/test

我相信它应该显示

Dude, that's a 404 !

我可能在SO论坛上搜索过,但是有人对此异常行为有解释吗?

I might have missearched the SO forum, but does anyone have an explanation on this strange behavior ?

@Mohammad Walid

好的,所以,我将route.php更改为:

Okay, so, I changed my routes.php to this :

Route::get('/', 'HomeController@showWelcome');

Route::get('test', function () {
    return 'test';
});

App::missing(function () {
    return Response::make("Dude, that's a 404 !", 404);
});

还没有删除了您在public/.htaccess中谈到的那一行.

I have not removed the line you talked about in public/.htaccess.

这有效:

base_url/test

但这不是(重定向到localhost/test):

But this doesn't (redirects to localhost/test) :

base_url/test/

怎么可能,因为您说过删除该行会删除该功能?

How can that be, since you said that removing the line would remove that feature ?

EIDT 2:

由于事实证明问题出在MAMP无法正确解释尾部斜杠重定向条件,因此在MAMP错误库中创建了一个错误报告:

Since it turns out that the problem comes from MAMP not interpreting the trailing slashes redirect condition correctly, a bug report was created in the MAMP bug base :

http://bugs.mamp.info/view.php?id=4586

推荐答案

它是因为默认情况下Laravel会重定向斜杠.

Its' because Laravel by default redirects trailing slashes.

app/public/.htaccess 文件中,您可以找到以下内容:

In app/public/.htaccess file you can find the following:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301] #This line causes the problem

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

将其更改为:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

看看跟踪斜线讨论.

注意,如果您删除上一行,则应用程序将无法处理斜杠. 前任.如果您有路线

Note if you remove the previous line your application won't be able to handle trailing slashes. Ex. if you have a route

Route::get('test', function() {
    return 'test';
}

假设已请求base_url/test/而不是base_url/test,则服务器将以404状态进行响应.

Assume base_url/test/ has been requested instead of base_url/test the server will respond with 404 status.

更新

这似乎是一个问题,仅在本地开发中才会发生(我使用XAMPP).

It seems that this is an issue only happens in local development (I use XAMPP).

该规则的预期结果如下:

The expected result for that rule is the following:

.htaccess
RewriteRule ^(.*)/$ /$1 [L,R=301]

Input (request)
http://localhost/website/test/

Output (after redirect)
http://localhost/website/test

但是我们实际上在本地开发中拥有什么:

But what we actually have in local development:

.htaccess
RewriteRule ^(.*)/$ /$1 [L,R=301]

Input (request)
http://localhost/website/test/

Output (after redirect)
http://localhost/test #this is wrong, according to the rule above it should redirects to http://localhost/website/test

因此,如果删除该规则,您将拥有什么:

So what you'll have if you remove the rule:

Request
http://localhost/website/test

Response
test


Request
http://localhost/website/test/

Response
Status 404
Dude, that's a 404 !

The expected response:
test

这就是我的意思

您的应用程序将无法处理斜杠.

your application won't be able to handle trailing slashes.

服务器将以404状态进行响应.

the server will respond with 404 status.

我已经使用在线htaccess测试器对该规则进行了测试,并且按预期运行,这就是为什么我认为问题只存在于当地发展中.

I have tested the rule with online htaccess tester and it worked as expected, that's why I suppose the problem only exists in local development.

这篇关于Laravel路由奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆