【问题标题】:laravel 4-call_user_func_array()laravel 4-call_user_func_array()
【发布时间】:2014-04-21 15:52:34
【问题描述】:

我正在尝试在数据库中插入一些数据。我已经创建了包含所有字段和控制器的表单。 CarController.php

public function create() {
        // load the create form (app/views/pages/create.blade.php)
        return View::make('pages.create');
    }

    public function store() {

            // store
            $data = new Car;
            $data->id = Input::get('id');
            $data->save();

            // redirect
            Session::flash('message', 'Successfully created data!');
            return Redirect::to('pages/cars');

    }

routes.php

Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController@show'));
Route::post('create', array('uses' => 'CarController@create'));
Route::post('store', array('store' => 'CarController@store'));

create.blade.php

 {{ Form::open(array('url' => 'store', 'class'=>'form-horizontal')) }}
            <div class="form-group">
                {{ Form::label('id', 'Vehicle ID',array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                {{ Form::text('id', Input::old('textId'), array('class' => 'form-control', 'placeholder'=>'Vehicle ID')) }}
                </div>
            </div>             
            {{ Form::submit('Create the Car!', array('class' => 'btn btn-primary')) }}
        </div>

问题是它显示以下错误: 期望参数 1 是一个有效的回调,没有给出数组或字符串 我不知道我做错了什么,因为我是 laravel 的新手

【问题讨论】:

  • 刚刚编辑了我的问题,因为这是现在显示的错误

标签: php html symfony laravel


【解决方案1】:

您正在使用资源控制器,这就是为什么您只需要一个路由声明,例如:

Route::resource('cars', 'CarController');

您还声明了以下路线:

Route::post('desc', array('uses' => 'CarController@show'));
Route::post('create', array('uses' => 'CarController@create'));
Route::post('store', array('store' => 'CarController@store'));

实际上你不需要这些路由声明,只使用第一个路由,这将为你的资源控制器创建路由。例如,您的路线如下所示:

Method |   Url         | Action | Route Name
--------------------------------------------
GET    | /cars/create  | create | cars.create // domain.com/cars/create using GET
POST   | /cars         | store  | cars.store  // domain.com/cars using POST

还有更多,你应该检查Resource Controllers。在这种情况下,您的控制器中有两个方法,如果您使用 url 之类的 yourdomain.com/cars/create 使用 GET 方法(如果您从浏览器的地址栏导航),那么它将调用 create 方法,如果您提交一个form 使用POST 方法到yourdomain.com/cars 然后它将调用store 方法并且您的所有表单字段都将在$_POST 数组中可用,您可以使用Input::get('id') 获取id 字段价值。查看相关信息以获取更多信息。

【讨论】:

    【解决方案2】:

    在 route.php 文件中更改这一行

    Route::post('store', array('store' => 'CarController@store'));
    

    Route::post('uses', array('uses' => 'CarController@store'));
    

    'uses'指定访问路由时调用哪个函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2017-08-07
      相关资源
      最近更新 更多