3

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 following 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.

Community
  • 1
  • 1
Student
  • 1,863
  • 9
  • 37
  • 50

5 Answers5

6

@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');
}
chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Sorry: `Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Resource matching "view" not found' in /var/www/student/library/Zend/Application/Bootstrap/BootstrapAbstract.php:692 Stack trace: ` – Student May 15 '11 at 00:52
  • I don't know what I am missing ? – Student May 15 '11 at 00:52
  • 4
    @Student To use this answer, you need to configure the view resource in your config file, eg `resources.view[] = ` or even better `resources.view.encoding = "utf-8"` – Phil May 15 '11 at 02:33
5

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Fatal error: Uncaught exception 'Zend_View_Exception' with message 'script 'layout.phtml' not found in path (/var/www/student/application/views/scripts/:/var/www/student/application/modules/institute/views/scripts/:./views/scripts/)' in /var/www/student/library/Zend/View/Abstract.php:980 Stack trace: #0 /var/www/student/library/Zend/View/Abstract.php(876): Zend_View_Abstract->_script('layout.phtml') #1 /var/www/student/library/Zend/Layout.php(796): – Student May 14 '11 at 17:53
  • @Student this means it is working. It tries to find your layout template in the default path but apparently, you didnt put one there. – Gordon May 14 '11 at 18:03
  • 2
    Just use the one from the original documentation as stated above. – BRampersad May 14 '11 at 20:23
2

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();
?>
Student
  • 1,863
  • 9
  • 37
  • 50
1
$this->title = "Edit album";
$this->headTitle($this->title);
Griwes
  • 8,805
  • 2
  • 43
  • 70
Jay Bharat
  • 691
  • 7
  • 6
1

You could try:

protected function _initViewHelpers() {         
    $bootstrap = $this->getBootstrap();
    $view = $bootstrap->getResource('view');
    $view->headTitle( 'My Title' );
}
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • Fatal error: Call to a member function getResource() on a non-object in /var/www/student/application/Bootstrap.php on line 9 – Student May 14 '11 at 17:54
  • `Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Invalid method "getBootstrap"` – Student May 14 '11 at 17:59
  • May be something missing in my application.ini. I can place my application.ini in my question if you want to see.. – Student May 14 '11 at 18:02