0

Say for example in mydefaultlayout.phtml i have an array declared in this way

$pages['words'] = array( 'APPLE', 'BALL', 'CAT', 'DOG', 'HELL', 'INK', 'PINK');
$pages['letters'] = array( 'A', 'B', 'C', 'D', 'H', 'I', 'K');

Mydefaultlayout.phtml has been my default layout in the entire application so it is available to every controller. So How do I access this elements $pages['words'] and $pages['letters'] in my controller.

How can i assign them to the view from the controller.

Class MyController extends Zend_controller_Action{
    public function indexAction(){
        if($_POST['page']=='ABC'){
            //How do i assign this $pages['words']  and $pages['letters']  to the view  
            $view->myarray = $pages['words'];
            $view->myarray = $pages['letters']; 
        }
    } 
}

One solution i have in mind is again redefine the $pages['words'] and $pages['letters'] in the controller and assign it to the view which is the repeatative code. How can i access the $pages['words'] and $pages['letters'] in the controller or view directly

2
  • just define the letters in the controller and assign it to the view? o.O Commented Sep 21, 2010 at 6:48
  • And if you need it in only some controllers, then an controller action helper can be useful. Commented Sep 21, 2010 at 23:51

3 Answers 3

1

you shouldn't access any view variable from controller.

better to define data $page['words'] and $page['latters'] at model, and access it when you need it from both controller and view.

Sign up to request clarification or add additional context in comments.

Comments

0

in your controller

public function indexAction(){
    $this->view->myarray = array('a','b','c','d');
}

in your layout

<?
foreach($this->myarray  as $key => $value ){
    echo " $key => $value  <br>";
}
?>

Comments

0

This is a little off the cuff, but why is the array defined in your layout?

If you need to have it available everywhere why not define it say in your bootstrap & then pass it to the views/controllers as and when you need?

So in your bootstrap you can have something like this:

$pages['words'] = array( 'APPLE', 'BALL', 'CAT', 'DOG', 'HELL', 'INK', 'PINK'); 

$pages['letters'] = array( 'A', 'B', 'C', 'D', 'H', 'I', 'K');

$view->pages = $pages;

Then in your views, you can access them:

print_r($this->pages);

Hope this is helpful, just going from memory & in a rush :)

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.