我在Controller类中有以下功能:
public function functionA(){
$name=Input::get('name');
$age=Input::get('age');
...
//the rest of the function
...
}
工作正常......
现在,我有另一个函数,其中参数作为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();
}
有人可以指出我正确的方向吗?
答案 0 :(得分:1)
您应该在laravel 5中使用Input::get()
,而不是使用Request()
。您可以将此注入到方法中:
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);
}