Laravel URL helper:如何使用查询参数和哈希生成完美的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28089664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:45:25  来源:igfitidea点击:

Laravel URL helper: How to generate a perfect URL with query parameters and hash

phplaravellaravel-4url-helperlaravel-helper

提问by Debiprasad

Suppose the route is like this:

假设路线是这样的:

Route::get('messages/{messages}', ['as' => 'messages.show', 'uses' => 'MessagesController@show']);

So, when we will create an URL using URL helper of Laravel,

所以,当我们使用 Laravel 的 URL helper 创建一个 URL 时,

{{ route('messages.show', 12) }}

will display example.com/messages/12.

将显示example.com/messages/12.

This is correct. Let's have some hash in the URL.

这是对的。让我们在 URL 中添加一些哈希值。

{{ route('messages.show', [12, '#reply_23']) }}

This will display example.com/messages/12#reply_23.

这将显示example.com/messages/12#reply_23.

This looks good. Now let's add some query strings instead of the hash.

这看起来不错。现在让我们添加一些查询字符串而不是哈希。

{{ route('messages.show', [12, 'ref=email']) }}

This will display example.com/messages/12?ref=email. This looks cool.

这将显示example.com/messages/12?ref=email. 这看起来很酷。

Now add both query string and hash.

现在添加查询字符串和哈希。

{{ route('messages.show', [12, 'ref=email', '#reply_23']) }}

Now this will display example.com/messages/12?ref=email&#reply_23. This looks little ugly because of the &in the URL. However it's not creating a lot of problem, I would like to get a clean URL like example.com/messages/12?ref=email#reply_23. Is there a way to get rid of the unnecessary &in the URL?

现在这将显示example.com/messages/12?ref=email&#reply_23. 由于&URL中的,这看起来有点难看。但是它并没有造成很多问题,我想获得一个干净的 URL,如example.com/messages/12?ref=email#reply_23. 有没有办法去掉&URL 中不必要的内容?

Edit:There is a workaround, but I am looking for a solid answer.

编辑:有一种解决方法,但我正在寻找可靠的答案。

<a href="{{ route('messages.show', [12, 'ref=email']) }}#reply_23">Link to view on website</a>

回答by Bogdan

The Laravel UrlGeneratorclass does not support specifying the #fragmentpart of the URL. The code responsible for building the URL is the following, and you can see it just appends the query string parameters and nothing else:

LaravelUrlGenerator类不支持指定#fragmentURL的一部分。负责构建 URL 的代码如下,您可以看到它只附加了查询字符串参数,没有其他任何内容:

$uri = strtr(rawurlencode($this->trimUrl(
            $root = $this->replaceRoot($route, $domain, $parameters),
            $this->replaceRouteParameters($route->uri(), $parameters)
        )), $this->dontEncode).$this->getRouteQueryString($parameters);

A quick test of your code reveals that the second example you posted:

对您的代码的快速测试显示您发布的第二个示例:

{{ route('messages.show', [12, '#reply_23']) }}

Actually generates:

实际生成:

/messages/12?#reply_23 // notice the "?" before "#reply_23"

So it treats #reply_23as a parameter rather than as a fragment.

所以它被视为#reply_23一个参数而不是一个片段。

An alternative to this shortcoming would be to write a custom helper function that allows to pass the fragment as a third parameter. You could create a file app/helpers.phpwith your custom function:

这个缺点的替代方法是编写一个自定义帮助函数,允许将片段作为第三个参数传递。您可以app/helpers.php使用自定义函数创建一个文件:

function route_with_fragment($name, $parameters = array(), $fragment = '', $absolute = true, $route = null)
{
    return route($name, $parameters, $absolute, $route) . $fragment;
}

Then add the following line at the end of your app/start/global.phpfile:

然后在app/start/global.php文件末尾添加以下行:

require app_path().'/helpers.php';

You can then use it like this:

然后你可以像这样使用它:

{{ route_with_fragment('messages.show', [12, 'ref=email'], '#reply_23') }}

Of course you can name the function whatever you want, if you feel the name I gave it is too long.

当然你可以随意命名函数,如果你觉得我给它起的名字太长了。