Zend Framework and include files in views

What path do you use for including common files in a ZF view?

An example would be a logo that is going to go on every page. Is there an easy way to include a header.php file in the index.php for a given view? I can’t get it to work at all.

my application structure looks like:

/application
  /controllers
    IndexController.php
  /models
  /views
    /home
       index.php
    /includes
       header.php
/library
  /Zend
/www
  .htaccess
  index.php
  /images

So in the views/home/index.php file, I want to include header.php and I’ll be rendering that page from the IndexController indexAction.

How do I accomplish that?

At a guess (and loads left out…)

<?php

class someController
{
    pageAction()
    {
        $view = new Zend_View;
        $indexController = new indexController;
        $view->header = $indexController->indexAction(true);
        // blah blah

        $view->render('home/index.php');
    }
}

?>

(and change your indexAction($return = false) method to use: “if ($return) return $output;”)

or better yet… redesign it so you are not dependant on a controller to create the header, but place the output generation separately.

OK, I’m gonna run the risk of sounding stupid here…but I don’t get your code at all. :slight_smile:

Here’s my index controller:

<?php

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $view = Zend::registry('view');
	echo $view->render('/home/index.php');
    }

    public function noRouteAction()
    {
        $this->_redirect('/');
    }
}

?>

Now, here’s my index.php in the home directory:


<?php
	include 'header.php';
?>
<h4>Home Page</h4>

<?php
	include 'footer.php';
?>

I had to add my include path (/application/controllers/views/includes) to my PHP path for the header/footer to resolve correctly, but it does work.

Is this the correct technique to use when using the Zend framework?

you can change to relative includes.

Going by your directory stucture, you can change the includes in index.php to:

include '../includes/header.php';

and like wise for footer :slight_smile:

Haven’t toyed with Zend_View, but you should be able to call render() from within a template file, relative to your base path:

<?=$this->render('header.php')?>
 ...
 <?=$this->render('footer.php')?>

BTW, it would be nice to have a configurable template file extension which can be omitted when calling render().

That did not work. I thought it would too, but PHP couldn’t find the file.

This didn’t work either. I think that in order for Zend_View to find it, I’ve got to set a script path. I’m going to mess around with it a bit, but I’ll just do it the way I got it working before if I have to.

Change it to relative from your bootstrap file (i.e. your www/index.php file)

include '../application/views/includes/header.php';

Or to use the render method:

echo $this->render('includes/header.php');

Jenk -

thanks! Very nice. Both of those worked. I opted for the latter, but again they both work.

I swear I tried that first one! Oh well, live and learn.

The Zend Framework is exactly what I was looking for in a PHP framework, so its nice to find solutions to these everyday type of problems.