3

I have a problem with my form in laravel, So my project structure is:

controllers/
       administration/
            NewsController.php

in NewsController I have a method call : postCreate():

 public function postCreate(){
    $validator = Validator::make(Input::all(), \News::$rules);
    if($validator->passes()){
        $news = new \News();
        $news->title = Input::get('title');
        $news->content = Input::get('content');
        $news->author = Input::get('author');
        $news->type = Input::get('type');

        $image = Input::file('file');
        $filename = time().".".$image->getClientOriginalExtension();
        $path = public_path('content/images/' . $filename);
        Image::make($image->getRealPath())->resize(468,249)->save($path);
        $news->image = 'content/images/'.$filename;
        $news->save();

        return Redirect::to('/administration/news/add')
            ->with('message','Succes');
    }
    return Redirect::to('/administration/news/add')
        ->with('message','Error')
        ->withErrors($validator)
        ->withInput();
}

My form have action :

{{ Form::open(array('url'=>'administration/news/create', 'files'=>true)) }}
{{ Form::close() }}

My route:

Route::post('/administration/news/create', array('uses'=>'App\Controllers\Administration \NewsController@postCreate'));

But when I submit I get an error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

I don't understand where is my problem.

Harea Costea
  • 275
  • 5
  • 19

2 Answers2

1

You have a whitespace in your code. Your route should be:

Route::post('/administration/news/create', array('uses'=>'App\Controllers\Administration\NewsController@postCreate'));

Besides that, altough laravel gives you standard a POST action, its always better to add a POST action to your form.

'method' => 'post'
Chilion
  • 4,380
  • 4
  • 33
  • 48
1

A small adjustment.... forget manually creating addresses.

In routes.php:

Route::post('/administration/news/create', 
          array('uses'=>'App\Controllers\Administration\NewsController@postCreate',
                 'as' => 'news.post.create'));

In View:

{{ Form::open(array('url'=>route('news.post.create'), 'files'=>true)) }}

no need to memorise any of those addresses.

itachi
  • 6,323
  • 3
  • 30
  • 40