Laravel 5:输入参数设置为其他功能


Laravel 5: Set Input parameters to another function

我在我的控制器类中有以下函数:

public function functionA(){
   $name=Input::get('name');
   $age=Input::get('age');
   ...
   //the rest of the function
   ...
}

It works fine…

现在,我有另一个函数,其中参数作为JSON传递:

public function functionB(){
    $params = json_decode(file_get_contents("php://input"));
    $name = isset($params->name) ? trim($params->name) : "";
    $age = isset($params->age) ? trim($params->age) : "";
    //I want to do this to save having to write functionA twice:
    Input::set('xxx'... ??? Can I do this?
    $this->functionA();
}
谁能告诉我正确的方向?

在laravel 5中应该使用Request()而不是使用Input::get()。你可以把它注入到你的方法中:

public function functionA('Illuminate'Http'Request $request){
   $name=$request->name;
   $age=$request->age;
   ...
   //the rest of the function
   ...
}

之后,你的问题变得相当模糊,但我猜你是从函数a内调用函数B,所以你可以简单地传递$request对象在那里:

public function functionB('Illuminate'Http'Request $request){
    $params = $request; // Not needed, you can simply use $request below...
    $name = isset($params->name) ? trim($params->name) : "";
    $age = isset($params->age) ? trim($params->age) : "";
    //I want to do this to save having to write functionA twice:
    $request->xxx = 'whatever you want';
    $this->functionA($request);
}