2
votes

i'm makeing my 1st app whith Zend and i have got a problem.

zftutorial.dev\application\controllers\IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }
    public function indexAction()
    {
        $this->view->title = "My Albums"; 
    }
}

zftutorial.dev/application/views/scripts/index/index.phtml

<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->render('footer.phtml'); ?>

zf-tutorial/application/views/scripts/header.phtml

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<div id="content">

zf-tutorial/application/views/scripts/footer.phtml

      </div>
   </body>
</html>

but when i go to address

http://zftutorial.dev/

i get in my browser this info:

An error occurred

Application error

Offcourse i set up vhosts files and when i using

zftutorial.dev/application/views/scripts/index/index.phtml

<h1><?php echo $this->escape($this->title); ?></h1>

then everything is ok. Problem is whith this <?php echo $this->render('header.phtml'); ?> and this <?php echo $this->render('footer.phtml'); ?> line but i don't know what is wrong.

3
Where have you stored header.phtml and footer.phtml in your project? - Jay Bhatt

3 Answers

1
votes

Try this:

Create the header.php and footer.php in the folder layouts/scripts/.

Then in your layout.phtml render the header and footer scripts.

layout.phtml

<html>
<head>
<?php echo $this -> headTitle(); //get the title ?>
</head>
<body>
<?php echo $this->render('header.phtml'); //relative to layouts/scripts ?>
<?php echo $this->layout()->content; //the view script redered in the controller action?>
<?php echo $this->render('footer.phtml'); //relative to layouts/scripts ?>
</body>

Bootstrap.php

Add this function:

protected function _initViewHelpers() {
    $this->bootstrap('view');
    $view = $this->getResource('view');

    //Page Base Head Title
    $view -> headTitle('My Domain');
    //Page Head Title - Seperator
    $view -> headTitle() -> setSeparator(' - ');

    return $view;
}

MyController.php

Inside your Controller Action or inside the init() function, add the following line:

//Prepended - My Domain
$this->view->headTitle()->prepend('Prepended');

//My Domain - Appended
$this->view->headTitle()->append('Appended');
1
votes

I think that use a layout is the better solution.

But with your code, you can do that:
In zftutorial.dev/application/views/scripts/index/index.phtml

<?php echo $this->partial('header.phtml', array('title' => $this->title)); ?>
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->partial('footer.phtml'); ?>
0
votes

It seems that your views aren't registered with ZF. Try registering them by placing the following method in your Bootstrap class.

   public function _initViewScripts(){
         $this->bootstrap ( 'view' );
         $view = $this->getResource('view');
         //Replace layout/scripts with the folder which contains your header.phtml and footer.phtml
         $view->addScriptPath (APPLICATION_PATH. '/layouts/scripts/');
   }

Also if you have placed the header and footer view inside /application/views/scripts/index/ directory then move them to a higher directory like /layouts/scripts/