我目前在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)
{
//
}
}
而你的路线? –
你能提供你的路线吗?可能你在路线中使用了错误的方法(取而代之)。 –
'Route :: get('contact','PagesController @ getContact'); Route :: get('about','PagesController @ getAbout'); Route :: get('/','PagesController @ getIndex'); Route :: resource('posts','PostController');' – Denisx