简体   繁体   English

RouteCollection.php第218:4行中的MethodNotAllowedHttpException

[英]MethodNotAllowedHttpException in RouteCollection.php line 218:4

I will get MethodNotAllowedHttpException when submitting a form in laravel 在Laravel中提交表单时,我将获得MethodNotAllowedHttpException

Html file HTML文件

<form method="POST" action="/cards/{{$card->id}}/notes">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    <textarea name="body" class="form-control"></textarea>
    <button type="submit">Add Note</button>
</form>

routes.php routes.php文件

Route::post('cards/{card}/notes','NotesController@store');

NotesController.php NotesController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;

class NotesController extends Controller
{
    public function store()
    {
        return request()->all();
    }
}

Make sure you don't have a route, say a Route::post with a parameter that lies in front of the route you are trying to hit. 确保您没有路线,例如说一个Route::post ,其参数位于您要点击的路线的前面。

For example: 例如:

Route::post('{something}', 'SomethingController@index');
Route::post('cards/{card}/notes', 'NotesController@store');

In this case, no matter what you try to send to the cards route, it will always hit the something route because {something} is intercepting cards as a valid parameter and triggers the SomethingController . 在这种情况下,无论您尝试将什么发送到纸牌路线,它都将始终命中something条路线,因为{something}会将cards作为有效参数进行拦截并触发SomethingController Put the something route below the cards route and it should work. something路线放在纸牌路线下方,它应该可以工作。

MethodNotAllowedHttpException is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found. 如果找不到匹配的路由(方法和URI),但是找到了具有匹配的URI但没有匹配方法的路由,则抛出MethodNotAllowedHttpException

In your case, I guess the issue is because URI parameters differ between the route and the controller. 在您的情况下,我想这是因为路由和控制器之间的URI参数不同。

Here are two alternatives you can try: 您可以尝试以下两种选择:

  1. Remove the parameter from your route: 从您的路线中删除参数:
Route::post('cards/notes','NotesController@store');
  1. Add the parameter to your controller: 将参数添加到您的控制器:
public function store($card)
    {
        return request()->all();
    }

I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem. 我试图解决流明中的这个错误,花了我很多时间才能解决这个问题。 The problem is with laravel itself. 问题出在laravel本身。

Sometimes if you have another route like GET device/{variable}, laravel stops in this first route... 有时,如果您有另一条路线,例如GET device / {variable},laravel会在第一条路线中停下...

So what you need to do is change the route POST device to POST device/add 因此,您需要做的是将路由POST device更改为POST device/add

This link helped me a lot 该链接对我帮助很大

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RouteCollection.php第218行中的Laravel MethodNotAllowedHttpException: - Laravel MethodNotAllowedHttpException in RouteCollection.php line 218: routecollection.php第218行中的methodnotallowedhttpexception - methodnotallowedhttpexception in routecollection.php line 218 Laravel中RouteCollection.php第218行的MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php line 218 in Laravel RouteCollection.php第218行中的MethodNotAllowedHttpException我该怎么办? - MethodNotAllowedHttpException in RouteCollection.php line 218 what can I do? RouteCollection.php 第 218 行中的 Laravel 5.2.36 MethodNotAllowedHttpException: - Laravel 5.2.36 MethodNotAllowedHttpException in RouteCollection.php line 218: Laravel表单提交在RouteCollection.php第218行显示MethodNotAllowedHttpException: - Laravel form submit showing MethodNotAllowedHttpException in RouteCollection.php line 218: RouteCollection.php第218行中的MethodNotAllowedHttpException关于更新数据的laravel 5.2 - MethodNotAllowedHttpException in RouteCollection.php line 218 laravel 5.2 on update data 如何解决RouteCollection.php第218行中的MethodNotAllowedHttpException :? - How to solve MethodNotAllowedHttpException in RouteCollection.php line 218:? laravel 5.3 RouteCollection.php第218行中的MethodNotAllowedHttpException: - laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218: RouteCollection.php第218行中的Laravel XMLHttpRequest MethodNotAllowedHttpException: - Laravel XMLHttpRequest MethodNotAllowedHttpException in RouteCollection.php line 218:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM