Friday, 6 September 2013

Getting MethodNotAllowedHttpException in Laravel4

Getting MethodNotAllowedHttpException in Laravel4

I'm a beginner to Laravel4, I've modified all my routes to use route
names, And modified most of my controllers and added get or post in front
of each action.
But when i try to access addPost route i get MethodNotAllowedHttpException
exception !
Here's my routes :
Route::group(array('prefix' => 'post'), function(){
Route::get('/', array('as' => 'listAllPosts', 'uses'
=> "PostController@listPosts"));
Route::get('list', array('as' => 'listAllPosts', 'uses'
=> "PostController@listPosts"));
Route::get('list/{id}', array('as' => 'listSinglePost', 'uses'
=> "PostController@showPost"));
Route::get('add', array('as' => 'addPost', 'uses'
=> "PostController@getAddPost"));
Route::get('edit/{id}', array('as' => 'editPost', 'uses'
=> 'PostController@getEditPost'));
Route::get('comment/edit/{id}', array('as' => 'editComment', 'uses'
=> 'CommentController@getEditComment'));
Route::group(array('before' => 'csrf'), function(){
Route::post('addcomment/{post_id}', array('as' =>
'addComment', 'uses' => 'CommentController@postAddComment'));
Route::post('deletecomment/{comment_id}', array('as' =>
'deleteComment', 'uses' => 'CommentController@deleteComment'));
Route::post('add', array('as' =>
'addPost', 'uses' => 'PostController@postAddPost'));
Route::post('edit', array('as' =>
'editPost', 'uses' => "PostController@postEditPost"));
Route::post('delete/{id}', array('as' =>
'deletePost', 'uses' => "PostController@deletePost"));
});
});
And my Controllers :
class PostController extends BaseController {
public function listPosts(){
$posts = Post::all();
return View::make('admin.post.list')->with('posts' , $posts);
}
public function showPost($id){
if(!is_numeric(trim($id))){
return Redirect::action('PostController@listPosts');
}
$post = Post::find($id);
if(empty($post)){
return Redirect::action('PostController@listPosts');
}
return View::make('admin.post.postdetails')->with('post', $post);
}
public function getAddPost(){
return View::make('admin.post.add');
}
public function postAddPost(){
$data = Input::all();
$rules = array(
'title' => 'required|min:3',
'body' => 'required|min:10',
);
$validator = Validator::make($data, $rules);
if($validator->passes()){
$post = new Post();
$post->title = htmlentities(trim($data['title']));
$post->body = strip_tags($data['body'], '<strong><pre>');
$post->save();
return View::make('admin.post.add')->with('message' , 'Post
successfuly added.');
} else {
return
Redirect::to('admin/post/add')->withErrors($validator)->withInput();
}
}
public function getEditPost($id){
if(!is_numeric($id)){
return Redirect::route('listAllPosts');
}
$post = Post::find($id);
if(empty($post)){
return Redirect::route('listAllPosts');
}
return View::make('admin.post.edit')->with('post', $post);
}
public function postEditPost(){
// $data = Input::all();
// $rules = array(
// 'title' => 'required|min:3',
// 'body' => 'required|min:10',
// );
// $validator = Validator::make($data, $rules);
// if($validator->passes()){
// $post = Post::find($id);
// $post->title = htmlentities(trim($data['title']));
// $post->body = strip_tags($data['body'], '<strong><pre>');
// $post->save();
// return View::make('admin.post.postdetails')->with('post' ,
$post);
// } else {
// return
Redirect::to('admin/post/postdetails')->withErrors($validator)->withInput();
// }
}
public function deletePost($id){
$post = Post::find($id);
if(isset($post)){
$post->delete();
return
Redirect::action('PostController@listPosts')->with('message' ,
'Post successfuly deleted.');
} else {
return
Redirect::action('PostController@listPosts')->with('message' ,
'Error while deleting post.');
}
}
}
And my admin.post.add View :
@extends('layout.layout')
@section('header')
@stop
@section('content')
<h2>Main - Admin - Add Post</h2>
<ul>
@if(isset($message))
<p>{{ $message }}</p>
@endif
{{ Form::open(array('action' => 'PostController@postAddPost')) }}
{{ Form::Label('title', 'Title: ') }}
<br>
{{ Form::text('title', '') }}
{{ $errors->first('title') }}
<br>
{{ Form::Label('body', 'Body: ') }}
<br>
{{ Form::textarea('body', '') }}
{{ $errors->first('body') }}
<br>
{{ Form::submit('Submit') }}
{{ Form::close() }}
<li>{{ link_to_route('listAllPosts', 'Back') }}</li>
</ul>
@stop

No comments:

Post a Comment