I am new to laravel..I am trying to implement a simple blog web. The code of index.blade.php is as follows:
@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
the code of 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');
}
the code of 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) }}
When I click the delete button, things work well. I can delete the article. But when I click the show button, I get the MethodNotAllowedHttpException error. If I change the code in index.blade.php:
{{ Form::open(array('route' => array('articles.show', $article->id))) }}
{{ Form::submit('SHOW') }}
{{ Form::close() }}
to
{{ link_to_route('articles.show', 'Show', $article->id) }}
everything goes well. Can anyone tell me what's wrong with my form? Thanks in advance!
I don't know if it is too late, but i think i know what happens:
MethodNotAllowedHttpException occurs when you call certain resource with incorrect verb. If you have a route like 'api/user' with GET and POST, if u make a PUT call this will throw an MethodNotAllowedHttpException.
In the DELETE form you said 'method = DELETE' but in the SHOW case you didn't specify anything, so the default method (POST) was applied. In your routes.php you have the resource, but nothing for the verb POST, so Laravel throws a MethodNotAllowedHttpException.
The default method of 'link_to_resource' is GET ^^
How to solve: Add the method=GET in the Form::open