Kishore
Kishore

Reputation: 1912

Passing data from view in controller in Zend

I am new to Zend and have a question regarding Zend Framework. I tried to google it but didn't get the right answer.

A previous developer did something like this in controller

public function indexAction()
{
    $abc = $this->view->abc;
}

My question is how can you assign something from view in a controller? If you can do so is this a legal assignment?

Upvotes: 0

Views: 299

Answers (1)

Phil
Phil

Reputation: 164739

Whilst this is indeed a bad approach, I can provide a possible solution as to how this works.

My guess is, your previous developer is assigning some view properties early in the dispatch cycle, possibly even in Bootstrap, eg

// Bootstrap.php

protected function _initGlobalViewProperties()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->abc = 'abc';
}

Whilst there's really no issue with doing this, the view shouldn't be relied upon to provide resources to the controller. A better approach would be to create an application resource which is available to all controllers.

In Bootstrap.php...

protected function _initAbc()
{
    $resource = 'abc'; // can be anything

    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->abc = $resource;

    return $resource; // adds resource into Application registry
}

and in your controller...

$abc = $this->getInvokeArg('bootstrap')->getResource('abc');

Upvotes: 2

Related Questions