Zend: How to get view in Bootstrap.php?
I want to set the title of a web page in Bootstrap. I do something like this in Bootstrap.php:
protected function _initViewHelpers() {
$view = Zend_Layout::getMvcInstance()->getView();
$view->headTitle( 'My Title' );
}
I am getting fol开发者_如何学运维lowing error:
Fatal error: Call to a member function getView() on a non-object in /var/www/student/application/Bootstrap.php on line 7
How can I get the view? I have also tried this.
@ArneRie is close but got the syntax wrong. This is from the quickstart:
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
// but what you really want is
$view->headTitle('My title');
}
You are likely trying to access the instance before it was started. Try
protected function _initViewHelpers() {
$view = Zend_Layout::startMvc()->getView();
$view->headTitle( 'My Title' );
}
However, startMVC
can be passed an $options
argument, which can either be the path to the layout folder as a string or an array or Zend_Config
instance that will be set to the MVC instance. ZF will usually pass that in automatically at a later stage from your application.ini. I dont know how your application will behave when you dont pass that in.
A better choice would be to have a Resource Plugin or a Controller Plugin. See the linked pages for examples of those and also see the source code for Zend_Layout
for implementation details.
It is working for me now:
In Bootstrap.php
protected function _initViewHelpers() {
$view = new Zend_View();
$view->headTitle('Main Title')->setSeparator(' - ');
}
In any view/.phtml
<?php
$this->headTitle()->prepend('Page Title');
echo $this->headTitle();
?>
You could try:
protected function _initViewHelpers() {
$bootstrap = $this->getBootstrap();
$view = $bootstrap->getResource('view');
$view->headTitle( 'My Title' );
}
$this->title = "Edit album";
$this->headTitle($this->title);
精彩评论