Getting a zend view property using a helper called in a layout script

I wrote an assistant to make my main navigator. Part of this helper checks if a user is registered, checking if the user has been assigned to submit

if($this->view->user)
{
//do stuff
}

However, when I call this helper in my script layout, it cannot access this user -> view->; it is NULL (although var_dump ($ this-> view returns a view object, jsut does not have all the properties assigned to it). It can only access user data if I assign its layout in the script view, i.e.

$this->layout()->user = $this->user;

And then in the assistant do

if($this->view->layout()->user)
{
//do stuff
}

Which is a pain as it means in every view of the script I need to add a line to pass data to the layout.

, script, ?

*

:

    <?php

class Zend_View_Helper_MainNav extends Zend_View_Helper_Abstract {

        public function mainNav()
    {
        if ($this->view->layout()->navType == 'none'){
            $html = '<div id="nav"><a href="/" id="logo">Sum puzzles</a></div>';
        } else if($this->view->layout()->navType == 'small'){

                $html = '<div id="nav" class="small"><a href="/" id="logo">Sum puzzles</a><ul>' .
                                    '<li><a href="/puzzle/view" class="button">play a puzzle</a></li>' .
                                        '<li><a href="/sequence/play" class="button">puzzle sequences</a></li>' .
                                        '<li><a href="/puzzle/create" class="button">create a puzzle</a></li>';

                if(!$this->view->user)
                {
                    $html .=    '<li><a href="/teachers" class="button">teachers area</a></li>';
                }   
                $html .='</ul></div>'; 

        } else if($this->view->layout()->navType == 'large'){
                $html = '<div id="nav" class="large"><a href="/" id="logo">Sum puzzles</a><ul>' .
                                        '<li><a href="/sequence/play" class="playSequence">Play a sequence of puzzles</a></li>' .
                                        '<li><a href="/puzzle/create" class="createPuzzle">Create your own puzzles</a></li>';
                if(!$this->view->user)
                {
                    $html .= '<li><a href="/teachers" class="teachersArea">teachers area</a></li>';
                }
                    $html .=    '</ul></div>'; 
        }   
        return $html;
    }
}
+3
6

,

application.ini

resources.view[] =

, Zend, - -.

.

0

,

////////////////////////////////////////////////
// application/controllers/IndexController.php
////////////////////////////////////////////////
<?php
class IndexController extends Zend_Controller_Action
{
    public function init() {
        Zend_Layout::startMvc() ;
        $this->view->test_h1 = "H1 test value" ;
    }
    public function indexAction()
    {
        $this->view->test_h2 = "H2 test value" ;
    }
}

////////////////////////////////////////////////
// application/views/helpers/Qwer.php
////////////////////////////////////////////////
<?php
class Zend_View_Helper_Qwer extends Zend_View_Helper_Abstract {
    public function qwer() {
        return "<h1>{$this->view->test_h1}</h1>"
            . "<h2>{$this->view->test_h2}</h2>" ;
    }
}

////////////////////////////////////////////////
// application/views/scripts/layout.phtml
////////////////////////////////////////////////
<?php echo $this->qwer() ?>

////////////////////////////////////////////////
// Output
////////////////////////////////////////////////
<h1>H1 test value</h1><h2>H2 test value</h2>
+1

Zend_View , setView .

class Ldm_View_Helper_SubMenu
{
    public function subMenu($items)
    {
        // Build SubMenu
        $subMenu = '';

        $template = '
            <ul id="subnav">
                %s
            </ul>';

        $rowTemplate = '
                <li>
                    <a href="%s" %s>%s</a>
                </li>';

        $rows = '';

        foreach ($items as $key => $values)
        {
            $rows .= sprintf($rowTemplate, 
                             $values['url'],
                             (($this->view->action == $key) ? 'class="selected"' : ''),
                             $values['name']);
        }

        $subMenu = sprintf($template, $rows);

        return $subMenu;
    }

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
0

, -. :

// in your controller action
$this->view->test = 'testing 123';

// at the start of your helper method, called from your layout
echo $this->view->test;die;

-?

0

.

, .

0
$this->_helper->layout->assign('vars', $val);
$this->view->layout()->vars;
-2
source

Source: https://habr.com/ru/post/1725900/


All Articles