0

有几种方法可以访问控制器中的请求参数:

直接

class MyController extends \CController
{
    ...
    public function actionMy()
    {
        ...
        $myVar = $_GET['my_param'];
        ...
    }
    ...
}

在请求对象上

class MyController extends \CController
{
    ...
    public function actionMy()
    {
        ...
        $myVar = $this->request->params('my_param');
        ...
    }
    ...
}

(为什么)带有请求对象的变体更好吗?

4

1 回答 1

1

Web Request 类表示一个 HTTP 请求,它封装了 $_SERVER 变量并解决了不同 Web 服务器之间的不一致问题。

/**
 * Returns the named GET or POST parameter value.
 * If the GET or POST parameter does not exist, the second parameter to this method will be returned.
 * If both GET and POST contains such a named parameter, the GET parameter takes precedence.
 * @param string $name the GET parameter name
 * @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
 * @return mixed the GET parameter value
 * @see getQuery
 * @see getPost
 */
public function getParam($name,$defaultValue=null)
{
    return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $defaultValue);
}
于 2014-08-13T16:52:14.010 回答