0

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?

6
  • You only assign from controller to view, not from view to controller. Commented Oct 25, 2011 at 21:45
  • I know that you only assign from controller to view but my question was my previous developer did this so does this have any relavence in zend? or the whole assignment is wrong Commented Oct 25, 2011 at 21:52
  • But what's your question then? Doesn't it work? Commented Oct 25, 2011 at 21:59
  • it works fine and its all over the place my question is is this assignment legal? if the assignment possible? Commented Oct 25, 2011 at 22:00
  • 2
    @Kishore: legal in the sense of "it works" - yes; legal in the sense of good design: NO Commented Oct 25, 2011 at 22:02

1 Answer 1

2

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.