I've written a helper in order to make my main nav. Part of this helper checks if there is a user logged in by checking if a user has been assigned to the view
if($this->view->user)
{
//do stuff
}
However when I call this helper in my layout script it is unable to access $this->view->user; it has NULL value (although var_dump($this->view returns the view object, jsut not with all the properties I assigned to it). It can only access the user data if, in the view script, I assign it to the layout, i.e.
$this->layout()->user = $this->user;
And then in the helper do
if($this->view->layout()->user)
{
//do stuff
}
Which is a pain as it means in each view script I have to add a line to pass the data to the layout.
Is there a way to enable helpers called in the layout script to access properties of the view?
edit*
Here's the source:
<?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>&l开发者_JAVA技巧t;/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;
}
}
I created a basic example, which should help you
////////////////////////////////////////////////
// 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>
Zend_View will inject itself into the helper if you create a setView method in your helper class.
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;
}
I'm inclined to think you have a logic error somewhere. Try this:
// 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;
Did you get anything?
When you call the Helper the first time the view is set to the current view.
If you change views later it will not point to the correct view.
Finally got to the root of the problem
In my application.ini file I had the line
resources.view[] =
Which I think is a legacy from older versions of Zend, copied from some online tutorial somewhere.
Deleting that line resolved the problem.
$this->_helper->layout->assign('vars', $val);
$this->view->layout()->vars;
精彩评论