2015-02-09 142 views
1

我是laravel新手..我试图实现一个简单的博客网站。 index.blade.php的代码如下:laravel表单问题MethodNotAllowedHttpException错误

@foreach ($articles as $article) 
<div align = "center"> 
<h2>{{ $article->title }}</h2> 
<div>{{ $article->text }}</div> 
{{ Form::open(array('route' => array('articles.show', $article->id))) }} 
{{ Form::submit('SHOW') }} 
{{ Form::close() }} 

{{ Form::open(array('method' => 'DELETE', 'route' => array('articles.destroy', $article->id))) }} 
{{ Form::submit('DELETE') }} 
{{ Form::close() }} 
</div> 
@endforeach 

ArticlesController的代码:

public function show($id) 
{ 
$article = Article::find($id); 
return View::make('articles.show', compact('article')); 
} 

public function destroy($id) 
{ 
Article::destroy($id); 
return Redirect::route('articles.index'); 
} 

show.blade.php的代码:

<p> 
    <strong>Title:</strong> 
    {{ $article->title }} 
</p> 

<p> 
    <strong>Text:</strong> 
    {{ $article->text }} 
</p> 

{{ link_to_route('articles.index', 'Back') }} 
{{ link_to_route('articles.edit', 'Edit', $article->id) }} 

当我点击删除按钮,事情很好。我可以删除文章。但是当我点击显示按钮时,我得到了MethodNotAllowedHttpException错误。 如果我更改了index.blade.php代码:

{{ Form::open(array('route' => array('articles.show', $article->id))) }} 
{{ Form::submit('SHOW') }} 
{{ Form::close() }} 

{{ link_to_route('articles.show', 'Show', $article->id) }} 

一切顺利。任何人都可以告诉我我的表单有什么问题吗?提前致谢!

回答

0

我不知道这是否是为时已晚,但我想我知道发生了什么:当你调用不正确的动词一定的资源

MethodNotAllowedHttpException发生。如果您有GET和POST等'api/user'路由,如果您进行PUT调用,将会抛出一个MethodNotAllowedHttpException

在DELETE表单中,您表示'method = DELETE',但在SHOW示例中您没有指定任何内容,因此应用了默认方法(POST)。在您的routes.php中,您拥有资源,但对动词POST没有任何影响,因此Laravel会抛出一个MethodNotAllowedHttpException

'link_to_resource' 的默认方法是GET ^^

如何解决:添加方法= GET的形式::开

相关问题