Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 在控制器内模拟请求_Symfony - Fatal编程技术网

Symfony 在控制器内模拟请求

Symfony 在控制器内模拟请求,symfony,Symfony,我在一个symfony项目中工作,我的控制器有两个功能: function1Action ($request Request, $product) { $quantity = $request->request->get('quantity') //dothingshere } function2Action($product, $value) { $em = $this->getDoctrine()->getManager(); $pat

我在一个symfony项目中工作,我的控制器有两个功能:

function1Action ($request Request, $product) {
    $quantity = $request->request->get('quantity')
    //dothingshere
}
function2Action($product, $value) {
    $em = $this->getDoctrine()->getManager();
    $pattern = $em->getRepository('repo')->find($value)
    //return an array ['quantity'=>x]
    $this->function1Action($pattern, $product)
}
通常,用户使用good request参数调用函数1(post request)。所以这里一切都很好。我的问题是有时,函数2将被调用,当它被调用时,我需要调用函数1,但我没有正确的请求,我想发送
$pattern

所以我找到了3个解决方案

解决方案1: 创建function1bis,其操作与function1相同,但将数组作为参数

解决方案2:在我的第一个函数中初始化一个空值

function1 ($request Request, $product, $patt=null) {
    if(!$patt){
        $quantity = $request->request->get('quantity')
    }
    else {
        $quantity = $patt['quantity']
    }
    //dothingshere
}
function2($product, $value) {
    $em = $this->getDoctrine()->getManager();
    $pattern = $em->getRepository('repo')->find($value)
    //return an array ['quantity'=>x]
    $this->function1Action(null, $product, $pattern);
}
解决方案3: 在function2内创建对象请求


我试图做解决方案3,但我找不到如何做,我想知道希望其中一个是“最好的”,如果解决方案3编程不错,我最终做了选项1。看起来更符合逻辑,可以在其他时候使用。解决方案2似乎有风险,因为null参数可能会在其他地方导致问题,而解决方案3可能需要更多资源,因为我必须在我的函数2中执行foreach。因此,我的解决方案如下所示:

function1Action ($request Request, $product) {
    $quantity = $request->request->get('quantity')
    //dothingshere
}
function1bis($pattern, $product) {
    $quantity = $pattern['quantity']
    //dothingshere
}

function2Action($product, $value) {
    $em = $this->getDoctrine()->getManager();
    $pattern = $em->getRepository('repo')->find($value)
    //return an array ['quantity'=>x]
    $this->function1bis($pattern, $product)
}