【问题标题】:Form action in LaravelLaravel 中的表单动作
【发布时间】:2015-01-09 07:30:28
【问题描述】:

我在 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 以查看每个路由是否已正确声明。
  • 最好将表单发送到操作。这样 Laravel 会自动将表单设置为 postget
  • 你使用的是什么命名空间?

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


【解决方案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)) }}

无需记住这些地址中的任何一个。

【讨论】:

    【解决方案2】:

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

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

    除此之外,尽管 laravel 为您提供了标准的 POST 操作,但在表单中添加 POST 操作总是更好。

    'method' => 'post'
    

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2020-02-19
      • 2016-04-27
      • 2020-01-09
      • 2015-05-13
      • 1970-01-01
      • 2019-11-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多