不允许的方法 - 使用不正确的方法重定向

发布于 2025-02-12 05:21:46 字数 1256 浏览 3 评论 0 原文

我一直在研究简单的Laravel惯性VUE3应用程序。它有一个资源路线。

Route::resource('contact', \App\Http\Controllers\ContactController::class);

的路由联系。

这提供了命名

我的VUE组件中有一个使用形式的变量,其中一些分配的变量

let form = useForm({ ... });

我在同一组件中有一个提交方法,

let submit = () => {
    if(props.edit) {
        form.patch(`/contact/${props.contact.id}`);
    } else {
        form.post(`/contact`);
    }
}

这再次没有复杂。帖子方法正确解开并重定向

    Contact::query()->create($request->all());

    return redirect()->to(route('contact.index'));

以完全披露更新方法,请参见下文:

public function update(Request $request, Contact $contact): \Illuminate\Http\RedirectResponse
{
    $contact->fill($request->all())->save();

    return redirect()->to(route('contact.show', ['contact' => $contact]));
}

这与商店相同的方式工作。简单,然后重定向...但事实并非如此。

发生的事情是它运行补丁然后调用重定向 如果我使用索引路由(称为GET),则重定向通过以405结束来承载补丁方法。如果我使用 back()我会得到同样的东西。如果我使用表演路线,它将在循环中重定向,因为补丁路由使用相同的URL /Contact /{contect}

我在过去的5年中构建了Laravel应用程序,并且在使用命名路线之前没有这样的问题。我错过了基本的东西吗?尽管我不确定是什么,但它可能不是配置问题。

我正在使用WebPack手动安装Laravel 9.19,因为它已更改为当前版本的VITE。我没有vue错误或警告,也没有laravel日志。

I have been working on a simple Laravel Inertia Vue3 application. It has one resource route.

Route::resource('contact', \App\Http\Controllers\ContactController::class);

This provides the named routes contact.index .store .create .show .update .destroy and .edit

Nice and simple so far.

I have a useForm form variable in my Vue component with some assigned variables

let form = useForm({ ... });

I have a submit method in the same component

let submit = () => {
    if(props.edit) {
        form.patch(`/contact/${props.contact.id}`);
    } else {
        form.post(`/contact`);
    }
}

Again nothing complex. The post method fires off correctly and redirects

    Contact::query()->create($request->all());

    return redirect()->to(route('contact.index'));

For full disclosure of the update method, please see below:

public function update(Request $request, Contact $contact): \Illuminate\Http\RedirectResponse
{
    $contact->fill($request->all())->save();

    return redirect()->to(route('contact.show', ['contact' => $contact]));
}

This works in the same way as store. Simple and then redirects... but it doesn't.

What happens is that it runs the patch and then calls redirect
The redirect carries the patch method through ending up with a 405 if I use the index route (declared as get). If I use back() I get the same thing. If I use the show route, it redirects in a loop because the patch route uses the same URL /contact/{contact}

I have built Laravel applications for the last 5 years and have not had an issue like this before when using a named route. Have I missed something basic? If not its possibly a configuration issue although I am not sure what.

I am running Laravel 9.19 with webpack manually installed as its been changed to Vite on the current release. I have no Vue errors or warnings and no Laravel logs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2025-02-19 05:21:46

Laravel级别的PUT请求和补丁请求之间存在差异。
请运行PHP Artisan路线:列表 - 查看在您的情况下使用哪个路线,您有很大的可能使用put,而不是补丁:)

there is a difference between PUT and PATCH requests on laravel-level.
Please run php artisan route:list - and see which one is used in your case, There is a big chance that you using PUT, not patch :)

宣告ˉ结束 2025-02-19 05:21:46

太好了,ol'laravel再次吸引了我。

亚历山大·迪里亚文(Alexander Dyriavin)带着关于推杆和补丁的回答使我进入了正确的路线,但是,这并不是解决方案。

解决方案:

    form.transform((data) => ({
        ...data,
        _method: 'PUT' //spoof added to request
    })).post(`/contact/${props.contact.id}`); //sent as a post instead

Laravel文档允许您欺骗方法通过将它们发布 _method 字段。

简而言之,通过拉拉维尔(Laravel)的重定向,补丁程序或PUT请求总是会失败。过去,我会用Axios使用它们,并直接处理JSON响应。

So good ol' Laravel got me again.

Alexander Dyriavin got me on the right course with his answer about put and patch, however, it wasn't really the solution.

The solution:

    form.transform((data) => ({
        ...data,
        _method: 'PUT' //spoof added to request
    })).post(`/contact/${props.contact.id}`); //sent as a post instead

The Laravel docs allow you to spoof methods https://laravel.com/docs/5.0/routing#method-spoofing by posting them with a _method field.

Simply put, a patch or put request would have always failed with a redirect from Laravel. In the past I would have used them with Axios and handled a JSON response directly.

情泪▽动烟 2025-02-19 05:21:46

这次我真的在回答自己的问题。

我完全是个白痴,错过了建立惯性JS时的一步。我试图用使用法方法检索错误,发生的事情是我什么也没收到。

所以我会仔细检查文档。

原来,我错过了将此中间件添加到内核中的Web中间件组!

\App\Http\Middleware\HandleInertiaRequests::class,

我现在可以使用以前使用的.patch方法,而无需任何其他代码

This time I really am answering my own question.

I was a total idiot and missed a step when setting up inertia js. I was attempting to retrieve errors with the useform method and what happened was I received nothing.

So I though I would double check the docs.

Turns out I missed adding this middleware to the web middleware group in the kernel!

\App\Http\Middleware\HandleInertiaRequests::class,

I can now use the .patch method I had before and no need for any additional code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文