Student
Student

Reputation: 1863

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

Upvotes: 3

Views: 7986

Answers (5)

Jay Bharat
Jay Bharat

Reputation: 889

$this->title = "Edit album";
$this->headTitle($this->title);

Upvotes: 1

Student
Student

Reputation: 1863

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();
?>

Upvotes: 2

Gordon
Gordon

Reputation: 316989

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.

Upvotes: 5

chelmertz
chelmertz

Reputation: 20601

@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');
}

Upvotes: 6

homelessDevOps
homelessDevOps

Reputation: 20726

You could try:

protected function _initViewHelpers() {         
    $bootstrap = $this->getBootstrap();
    $view = $bootstrap->getResource('view');
    $view->headTitle( 'My Title' );
}

Upvotes: 1

Related Questions