【问题标题】:MethodNotAllowedHttpException while submitting form to process using laravel?使用laravel提交表单以处理时出现MethodNotAllowedHttpException?
【发布时间】:2017-03-28 02:53:59
【问题描述】:

我是 laravel 的新手,正在尝试创建一个简单的博客应用程序,我想将帖子详细信息存储到数据库中。我也使用 laravel 命令设置了我的数据库和表。 为了将数据存储到数据库中,我创建了以下表单:

@extends('main')

@section('title','| Create Post')
@endsection

@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1>Create New Post</h1>
        <hr/>
        <form action="store" method="POST">
            <div class="form-group">
                <div class="form-group">
                    <label name="title">Title:</label>
                    <input id="title" name="title" class="form-control">
                </div>
                <div class="form-group">
                    <label name="body">Post Body:</label>
                    <textarea id="body" name="body" class="form-control"></textarea>
                </div>
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="hidden" name="_method" value="POST">
                <input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
            </div>
        </form>
    </div>
</div>
@endsection

我为此表单提交请求创建了一个路由规则,如下所示:

 Route::resource('posts','PostController');

其中 PostController 是控制器,其中实现了所有必需的资源方法来处理和保存数据为

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller {

public function index()
{
    //
}

public function create()
{
    return view('posts.create');
}

public function store(Request $request)
{
    //validate the data
    $this->validate($request,array(
        'title' => 'required|max:255',
        'body' => 'required'
    ));

    //store in the database
    $post = new Post;
    $post->title = $request->title;
    $post->body = $request->body;
    $post->save();

    //redirect to page
    return redirect()->route('posts.show',$post->id);
}

public function show($id)
{
    //
}

public function edit($id)
{
    //
}

public function update($id)
{
    //
}

public function destroy($id)
{
    //
}

}

我还使用命令提示符列出了我的路线,它给了我以下输出:

App\Http\Controllers\PagesController@getIndex | | | |得到|头|帖子 |帖子索引 |应用\Http\Controllers\PostController@index | | | |得到|头|帖子/创建 |帖子创建 |应用\Http\Controllers\PostController@create | | | |发布 |帖子 |帖子商店 |应用\Http\Controllers\PostController@store | | | |得到|头|帖子/{帖子} |帖子显示 |应用\Http\Controllers\PostController@show | | | |得到|头|帖子/{帖子}/编辑 |帖子。编辑 |应用\Http\Controllers\PostController@编辑 | | | |放 |帖子/{帖子} |帖子更新 |应用\Http\Controllers\PostController@update | | | |补丁 |帖子/{帖子} | |应用\Http\Controllers\PostController@update | | | |删除 |帖子/{帖子} | post.destroy | App\Http\Controllers\PostController@destroy

提交保存详细信息的表单后,我收到 MethodNotAllowedHttpException。

【问题讨论】:

  • 你的输出告诉我你的create方法接受POST,而store方法需要GET

标签: php forms laravel-5


【解决方案1】:

试试这个:

@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1>Create New Post</h1>
        <hr/>
        <form action="{{ route('posts.store') }}" method="POST">
            <div class="form-group">
                <div class="form-group">
                    <label name="title">Title:</label>
                    <input id="title" name="title" class="form-control">
                </div>
                <div class="form-group">
                    <label name="body">Post Body:</label>
                    <textarea id="body" name="body" class="form-control"></textarea>
                </div>
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="hidden" name="_method" value="POST">
                <input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
            </div>
        </form>
    </div>
</div>
@endsection

Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多