Laravel - 如何使用哈希重定向 (#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38355156/
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 14:08:52  来源:igfitidea点击:

Laravel - How to redirect with hash (#)

laravel

提问by fico7489

I have link :

我有链接:

example.com/register#register

If validation fails laravel redirects to :

如果验证失败,laravel 会重定向到:

example.com/register

with validation errors bit without hash url part. How I can redirect to full url with # ?

带有验证错误位,没有哈希 url 部分。如何使用 # 重定向到完整网址?

I know I can use:

我知道我可以使用:

Redirect::to(route('register') . '#credits')

But I want complete solution so and my :

但我想要完整的解决方案所以和我的:

return back();

will redirect with #.

将重定向#。

Maybe I need to override some code ?

也许我需要覆盖一些代码?

回答by Ben Rolfe

You could create the URL first, using the route name.

您可以先使用路由名称创建 URL。

$url = URL::route('route_name', ['#hash_tag']);

Redirect::to($url);

Or...

或者...

return Redirect::to(URL::previous() . "#hash_tag");

回答by Picard

But if you want to get the proper URL where hash is the fragment part and not a parameter you should use:

但是如果你想获得正确的 URL,其中 hash 是片段部分而不是你应该使用的参数:

redirect(route('route_name', ['some_param_for_route']). '#hash')

instead of:

代替:

redirect()->route('route_name', [ 'some_param_for_route', '#hash' ])

so to get:

所以得到:

http://example.com/some_param_for_route#hash

and not:

并不是:

http://example.com/some_param_for_route?#hash

this way you can also chain it further like for instance:

通过这种方式,您还可以进一步链接它,例如:

redirect(route('route_name', ['some_param']). '#hash')->with('status', 'Profile updated!');

回答by Serhii Topolnytskyi

You could use helper:

你可以使用助手:

redirect()->route('route_name', [ 'some_param_for_route', '#hash' ])