2017-03-22 64 views
0

我目前在Laravel創建一個博客學習PHP/Laravel。問題是,即時得到:MethodNotAllowedException當我點擊按鈕重定向到另一個視圖

MethodNotAllowedHttpException在RouteCollection.php行218:

我有一個表,其中i顯示了所有按鈕「查看」每個職位的崗位和「編輯」。他們重定向到與id相關的觀點,他們工作得很好。當我打開任何帖子查看有一個編輯按鈕,這是我得到錯誤,當我點擊它,我不知道爲什麼。

show.blade.php //這是在 「視圖」 後

<div class="row"> 
    <div class="col-md-8"> 
     <h1>{{ $post->title }}</h1> 

     <p class="lead">{{ $post->body }}</p> 
    </div> 

    <div class="col-md-4"> 
     <div class="well"> 
      <dl class="dl-horizontal"> 
       <dt>Created at:</dt> 
       <dd>{{ date('d M, y H:i', strtotime($post->created_at)) }}</dd> 
      </dl> 
      <dl class="dl-horizontal"> 
       <dt>Last Updated:</dt> 
       <dd>{{ date('d M, y H:i', strtotime($post->updated_at)) }}</dd> 
      </dl> 
      <hr> 
      <div class="row"> 
       <div class="col-sm-6"> 
        <form method="POST" action="{{ route('posts.edit', $post->id) }}"> 
         <input type="submit" value="Edit" class="btn btn-primary btn-block"> 
         <input type="hidden" name="_token" value="{{ Session::token() }}"> 
        </form> 
       </div> 
       <div class="col-sm-6"> 
        <form method="POST" action="{{ route('posts.destroy', $post->id) }}"> 
         <input type="submit" value="Delete" class="btn btn-danger btn-block"> 
         <input type="hidden" name="_token" value="{{ Session::token() }}"> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

edit.blade.php

<div class="row"> 
    <form method="POST" action="{{ route('posts.update', $post->id) }}"> 
     <div class="row"> 
     <div class="col-md-8"> 
      <div class="form-group"> 
       <label for="title">Title:</label> 
       <textarea type="text" class="form-control input-lg" id="title" name="title" rows="1" 
          style="resize:none;">{{ $post->title }}</textarea> 
      </div> 
      <div class="form-group"> 
       <label for="body">Body:</label> 
       <textarea type="text" class="form-control input-lg" id="body" name="body" 
          rows="5">{{ $post->body }}</textarea> 
      </div> 
     </div> 
     <div class="col-md-4"> 
      <div class="well"> 
       <dl class="dl-horizontal"> 
        <dt>Created at:</dt> 
        <dd>{{ date('M j, Y h:i:sa', strtotime($post->created_at)) }}</dd> 
       </dl> 

       <dl class="dl-horizontal"> 
        <dt>Last updated:</dt> 
        <dd>{{ date('M j, Y h:i:sa', strtotime($post->updated_at)) }}</dd> 
       </dl> 
       <hr> 
       <div class="row"> 
        <div class="col-sm-6"> 
         <a href="{{ route('posts.show', $post->id) }}" class="btn btn-danger btn-block">Back</a> 
        </div> 
        <div class="col-sm-6"> 
         <button type="submit" class="btn btn-success btn-block">Save</button> 
         <input type="hidden" name="_token" value="{{ Session::token() }}"> 
     {{ method_field('PUT') }} 
        </div> 
       </div> 
      </div> 
     </div> 
     </div> 
    </form> 
</div> 

PostController.php //這是控制器,如果你還需要它

class PostController extends Controller { 

public function index() 
{ 
    // 
    $posts = Post::all(); 

    return view('posts/index')->withPosts($posts); 
} 

public function create() 
{ 
    // return create new post form 
    return view("posts/create"); 
} 

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

    // store data in db 
    $post = new Post; 

    $post->title = $request->title; 
    $post->body = $request->body; 

    $post->save(); 

    Session::flash('success', 'The blog post was successfully saved!'); 

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

public function show($id) 
{ 
    // 
    $post = Post::find($id); 

    // redirect to view 
    return view('posts/show')->with('post', $post); 

} 

public function edit($id) 
{ 
    // 
    $post = Post::find($id); 

    return view('posts/edit')->with('post', $post); 
} 

public function update(Request $request, $id) 
{ 
    // 
} 

public function destroy($id) 
{ 
    // 
} 
} 
+0

而你的路線? –

+0

你能提供你的路線嗎?可能你在路線中使用了錯誤的方法(取而代之)。 –

+0

'Route :: get('contact','PagesController @ getContact'); Route :: get('about','PagesController @ getAbout'); Route :: get('/','PagesController @ getIndex'); Route :: resource('posts','PostController');' – Denisx

回答

2

那是因爲你正在使用已定義的方法爲POST但路線被定義爲GET形式:

將其更改爲:

<form method="GET" action="{{ route('posts.edit', $post->id) }}"> 
    <input type="submit" value="Edit" class="btn btn-danger btn-block"> 
</form> 

此外,您還可以使用一個鏈接,而不是形式:

<a href="{{ route('posts.edit', $post->id) }}" class="btn btn-danger btn-block">Edit</a> 
+1

是的,我很盲目。謝謝。 – Denisx

相關問題