Laravel 中的表单操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26887102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form action in Laravel
提问by Harea Costea
I have a problem with my form in laravel, So my project structure is:
我在 laravel 中的表单有问题,所以我的项目结构是:
controllers/
administration/
NewsController.php
in NewsController I have a method call : postCreate():
在 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();
}
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
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
I don't understand where is my problem.
我不明白我的问题在哪里。
采纳答案by itachi
A small adjustment.... forget manually creating addresses.
一个小的调整......忘记手动创建地址。
In routes.php:
在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.
无需记住任何这些地址。
回答by Chilion
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.
除此之外,尽管 laravel 为您提供了标准的 POST 操作,但在表单中添加 POST 操作总是更好。
'method' => 'post'