Zend Framework - 在控制器中传递变量请求

时间:2012-02-22 12:12:41

标签: zend-framework

在其中一个php框架中,我注意到可以请求对象Request in action $ this-> request-> paramName

class MyRequest extends Zend_Controller_Request_Http{

    public $params = array();

    public function __construct() {
        $this->params = $this->getParams();
        parent::__construct();
    }

    public function __get($name) {
        if (isset($this->_params[$name])) {
            return $this->_params[$name];
        } 
    }

    public function __isset($name) {
        return isset($this->_params[$name]);
    }

}
在MyController中

我添加了变量请求

public $request = null;

如何将标准请求更改为我的标准?

public function __construct(
    Zend_Controller_Request_Abstract $request,
    Zend_Controller_Response_Abstract $response,
    array $invokeArgs = array()) {
        $request = new MyRequest();
        parent::__construct($request, $response, $invokeArgs);
        $this->request = $this->getRequest();
    }
  • 此功能未给出任何结果。

选项1是bootstrap中的make方法_initRequest():

protected function _initRequest() {
    $this->bootstrap ( 'FrontController' );
    $front = $this->getResource ( 'FrontController' );
    $request = $front->getRequest ();
    if (null === $front->getRequest ()) {
        $request = new MyRequest();
        $front->setRequest ( $request );
    }
    return $request;
}

2 个答案:

答案 0 :(得分:1)

有点脏和未经测试的解决方案。很想知道它是否有效。

//Bootstrap:

public function _initRequest()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    $front->setRequest(new MyRequest());
}

答案 1 :(得分:0)

试试这可能会对你有所帮助:

控制器文件中的

public function exampleAction(){   
    $result = $this->_request;

    $model  = new Employer_Model_JobInfo();        
    $results = $model->sample($result);
}

// the above line stores the values sent from the client side in $result.then sends that values to Model file with parameter $result   ..
模型文件中的

class Employer_Model_JobInfo extends Gears_Db_Table_Abstract{
    public function sample($param){

        $paramVal       = $param->getParam('name');
        $paramVal1       = $param->getParam('email');

    }
}

nameemail是用于将数据从客户端发送到服务器的名称。