在Laravel中的表单操作

3

我有一个关于 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

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


运行 php artisan routes 命令,查看每个路由是否已经正确声明。 - Matt Burrow
最好将表单发送到一个操作。这样,Laravel 将自动将表单设置为 postget - Jerodev
你正在使用哪个命名空间? - itachi
2个回答

1
你的代码中有空格。你的路由应该是:
Route::post('/administration/news/create', array('uses'=>'App\Controllers\Administration\NewsController@postCreate'));

除此之外,虽然 Laravel 提供了标准的 POST 操作,但最好还是在表单中添加一个 POST 操作。
'method' => 'post'

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)) }}

不需要记忆任何这些地址。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接