【问题标题】:Laravel 4.2 form Route POST error MethodNotAllowedHttpExceptionLaravel 4.2 表单路由 POST 错误 MethodNotAllowedHttpException
【发布时间】:2018-11-25 22:41:15
【问题描述】:

我已经使用 Laravel 有一段时间了,虽然我做了新安装,但我无法让路由 POST 工作,Routes GET 工作正常但不能发布。 它不断抛出 MethodNotAllowedHttpException 错误。 这是我的路线

Route::post('/admin/new_event', [ "as" => "admin.event_add", "uses" => "AdminController@adminEventAddPost" ]);

这是我的表格

<form action="{{ URL::route("admin.event_add") }}" method="POST"> <input type="text" placeholder="Event Title" name="title"><br/> <input type="text" placeholder="Event Detail" name="detail"><br/> <button type="submit" class="btn purp-button">Post</button> </form>

这是控制器

   public function adminEventAddPost()
{

    $title = Input::get("title");
    $detail = Input::get("detail");
    $date = date("Y-m-d");

    Events::create([
        "title" => $title,
        "detail" => $detail,
        "date" => $date
    ]);
    return Redirect::route("admin.events_new");
}

【问题讨论】:

    标签: php laravel laravel-4 routing


    【解决方案1】:

    尝试将{{csrf_field()}} 添加到您的表单中。

    【讨论】:

    • 我补充说然后它说找不到var。昨晚试过了。我已经阅读了很多关于 CSRF 的不同页面,但无法使其正常工作。
    • 调用未定义函数csrf_field()
    【解决方案2】:

    如果你还没有,看看这个

    https://laravel.com/docs/5.6/csrf

    这是关于 CSRF 保护的。

    就像 Algridas 在他的回答中所说,但我认为您使用的是旧版本的 laravel,您可能需要使用

    @csrf
    

    【讨论】:

    • 添加了这一点,但不像 @foreach 这样的页面命令中的其他命令 - 它实际上只是在表单元素上打印为文本
    • 我也试过这个&lt;input type="hidden" name="_token" value="{{csrf_token()}}"&gt; 但还是不行
    • @CasperRound 抱歉回复晚了,你试过 {{@csrf}} 吗?
    猜你喜欢
    • 2018-04-13
    • 2017-09-21
    • 2015-06-02
    • 2014-11-06
    • 2014-07-30
    • 2015-02-02
    • 2015-08-03
    • 1970-01-01
    • 2018-08-07
    相关资源
    最近更新 更多