Laravel中的路线是否带有正斜杠? [英] Routes in Laravel with or without a forward slash?

查看:57
本文介绍了Laravel中的路线是否带有正斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议的路线声明方法是什么:使用正斜杠还是将其排除在外更好?相对于其他使用,有什么好处吗?还是仅仅是偏爱问题?

What's the recommended approach for declaring routes: with a forward slash or is it better to leave it out? Are there any benefits to using one over the other or is it just a matter of preference?

使用它更好吗?

 Route::get('/read', function(){
        $user = User::findOrFail(1);
            return $user;
    });

或者这样:

Route::get('read', function(){
    $user = User::findOrFail(1);
        return $user;
});

谢谢.

推荐答案

它取决于偏好.通过路线时,它实际上会修剪掉前斜线,然后正确格式化它.在Illuminate/Routing/Router.php中,所有路由都通过 prefix 函数,如下所示:

It comes down to preference. When passing the route, it actually trims the forward slashes off, then formats it properly. In Illuminate/Routing/Router.php, all routes go through the prefix function, which looks like this:

protected function prefix($uri)
{
    return trim(trim($this->getLastGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/';
}

因此,如果您创建一个组前缀/test/和一个/route 的uri,它将成为 test/route

So if you create a group prefix /test/ and and a uri of /route, it becomes test/route

这篇关于Laravel中的路线是否带有正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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