在Laravel中形成行动

时间:2014-11-12 12:32:37

标签: php laravel laravel-4 php-5.3 laravel-routing

我的laravel表格有问题, 所以我的项目结构是:

controllers/
       administration/
            NewsController.php
在NewsController中我有一个方法调用: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();
}

我的表单有行动:

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

我的路线:

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

但是当我提交时,我收到了一个错误:

  

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

我不明白我的问题在哪里。

2 个答案:

答案 0 :(得分:1)

您的代码中有空格。你的路线应该是:

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

除此之外,altough laravel为您提供标准的POST操作,它总是更好地为您的表单添加POST操作。

'method' => 'post'

答案 1 :(得分:1)

小调整....忘记手动创建地址。

在routes.php中:

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

在视图中

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

无需记住任何这些地址。