atherdon
atherdon

Reputation: 39

Zend Framework - Passing a variable request in controller

In one of these php frameworks I've noticed a posibility to request the object Request in action as $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]);
    }

}

in MyController I've added variable request

public $request = null;

How can I change that standart Request to my one?

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();
    }

Option 1 is make method _initRequest() in bootstrap:

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;
}

Upvotes: 0

Views: 553

Answers (2)

bububaba
bububaba

Reputation: 2870

A bit dirty and untested solution. Would love to hear if it works.

//Bootstrap:

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

Upvotes: 1

mohan
mohan

Reputation: 453

Try this it might help you:

in controller file

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   ..

in model file

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

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

    }
}

The name and email are what name used to sent the data from client to server.

Upvotes: 0

Related Questions