zend 框架 2 return $this->redirect()

zend framework 2 return $this->rediect()

ex1:

// do something ...
return $this->redirect()->toRoute(..);
return false;

ex2:

public function myTest(){
     return $this->redirect()->toRoute(..);
}

// do something ...

myTest();
return false;

我什么时候使用 ex1,我的代码停止了 return,没有 运行 return false; ex2 下面的相同代码,return false 运行。 请帮帮我,为什么这样???

in ex1 return false; 未到达,因为 return 退出了您当前正在 运行ning 的函数。因此调用 $this->redirect()->toRoute(..) 将 运行,然后该函数的执行将结束。

ex2中,你定义了一个函数myTest(),所以return $this->redirect()->toRoute(..);退出myTest()函数,[=42] =] toRoute().

的值

然后下一行代码,return false 运行s 退出它所在的函数,值为false.

调用 return 后,该语句后面的任何代码都将被忽略。有点像 for 循环中的 break;continue; 语句。

您需要添加逻辑,例如 ifswitch 语句,并确定您是要 return false 还是 return $this->redirect()->toRoute(..);

例如:

function someFunction() {       // someFunctionCalled
    if (codingIsFun) {          // Coding is fun
        $foo = myTest();        // $foo is true, since myTest() returns true.
        return $foo;            // Exit "someFunction()" with a return value of $foo (true)
                                // any remaining code in "someFunction()" will not be executed.
    }

    // Some people will put this line in an "else" block,
    // but it isn't necessary, this code will only execute if 
    // coding is not fun.
    return false; // Coding is not fun.
}

function myTest() {
    return true;
}


// Call someFunction, if coding is fun, $isCodingFun will == true,
// If not, $isCodingFun will == false.
$isCodingFun = someFunction();