开发者

How do i access the array from Layout to Controller in Zend framework

开发者 https://www.devze.com 2023-01-16 18:31 出处:网络
Say for example in mydefaultlayout.phtml i have an array declared in this way $pages[\'words\'] = array( \'APPLE\', \'BALL\', \'CAT\', \'DOG\', \'HELL\', \'INK\', \'PINK\');

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开发者_StackOverflow社区. 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


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.


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


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消