我目前正在Laravel创建一个博客来学习PHP / Laravel。问题是我得到了:
RouteCollection.php第218行中的MethodNotAllowedHttpException:
我有一张桌子,我用按钮显示所有帖子" View"和"编辑"每个帖子。他们使用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 :(得分: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>