【问题标题】:Keep fragment in URL when redirecting from controller从控制器重定向时将片段保留在 URL 中
【发布时间】:2021-09-10 08:17:34
【问题描述】:

当从控制器重定向时,在存储(POST http 请求)之类的操作之后:

class EntityController extends Controller
{
    public function store(Request $request): RedirectResponse
    {
        ....
        return redirect()->route('entities.list')->withFragment('manage');
    }
}

如何在 URL 中保留哈希?

如上例,返回页面https://example.com/entities#manage

我怀疑 Inertia 在这种情况下正在做一些魔术,因为我无法将哈希值保留在 URL 中。从它在 Laravel 文档中所写的内容来看,它似乎应该是可能的。

相关技术栈:

  • Laravel 框架:8.42.1
  • inertiajs/inertia-laravel:v0.3.6

参考资料:

【问题讨论】:

    标签: laravel-8 inertiajs php-8


    【解决方案1】:

    您应该首先使用命名路由创建 URL。

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

    希望这会有用。

    【讨论】:

    • 我已经看到社区在几个论坛中提出了这种方法,但我不同意。这会生成一个类似example.com/route?#hash_tag 的URL,最好不要包含问号。我认为像$url = URL::route('route_name') . '#hash_tag'; 这样的方法更好。但是,我已经尝试过了,两者都不起作用
    • 您可以使用redirect('some-url')->withFragment('hash_tag') 方法将片段添加到重定向。
    • @erikgaal 这与我解释它不起作用的方法相同。你用的是同一个栈,Laravel 和 Inertia 吗?
    猜你喜欢
    • 2021-08-06
    • 2013-08-01
    • 2019-03-06
    • 1970-01-01
    • 2012-12-04
    • 2021-03-30
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多