2016-11-14 64 views
0

我是laravel的新手,尝试创建一个简单的应用程序,即博客,我想将一个帖子的详细信息存入数据库。我已经使用laravel命令设置了我的数据库和表。MethodNotAllowedHttpException同时提交表单以使用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 | | | | GET | HEAD |帖子| posts.index | App \ Http \ Controllers \ PostController @ index | | | | GET | HEAD | posts/create | posts.create | App \ Http \ Controllers \ PostController @ create | | | | POST |帖子| posts.store | App \ Http \ Controllers \ PostController @ store | | | | GET | HEAD |帖子/ {posts} | posts.show | App \ Http \ Controllers \ PostController @ show | | | | GET | HEAD |帖子/ {帖子} /编辑| posts.edit | App \ Http \ Controllers \ PostController @ edit | | | | PUT |帖子/ {posts} | posts.update | App \ Http \ Controllers \ PostController @ update | | | | PATCH |帖子/ {posts} | | App \ Http \ Controllers \ PostController @ update | | | |删除|帖子/ {posts} | posts.destroy | App \ Http \ Controllers \ PostController @ destroy

我在提交表单以保存细节后收到MethodNotAllowedHttpException。

+0

你的输出告诉我,你的'''create'''方法接受'''POST'''其中存储方法预计'''GET' '' –

回答

0

试试这个:

@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

+0

是的,谢谢先生,有用的答案和文档。 –

+0

高兴地帮助:) –