views:

1000

answers:

4

I'm pretty new to Zend.. I'm just wondering how to model my site's layout/structure. My site will have an user profile section, admin section, and the generic the default view of the site.

For the admin and profile, I'll have custom elements in the headers and footers, otherwise I want to default to a generic header/footer.

I want the ability to have an entirely custom skin separate from the default view, how should I structure things?

So far I have have created a directory in application/ named layouts. I have modified the application.ini file so it accounts for that:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

So my default layout view now exists in application/layouts/scripts/layout.phtml, I have the header and footer embedded in this layout.phtml file but I would like to strip them out and have them in separate files.

Could anyone assist me in coming up with the structure for this? So to re-iterate I want a custom default layout, custom admin layout, custom user profile layout, and for all 3 layouts I want customizable heading/footer "includes" but I'm not sure how this is done properly in Zend as I'm accustomed to include files.

+1  A: 

To have a different layout/config for various sections of a site, I use Modules with the following module setup. Module Config

[Edit]

Directory Structure :

/application
    /forms
    /models
    /modules
        /admin
            /config
            /contollers
            /layout
            /views
        /default
            /config
            /contollers
            /layout
            /views
piddl0r
How do modules differ from view helpers, or partial view helpers?
meder
I use this method because each module has it's own layout and config directory, meaning the controller doesn't set the alternative layout. I find it makes controlling access through ACls easier with modules too.Directory Structure : /application /forms /models /modules /admin /config /contollers /layout /views /default /config /contollers /layout /views
piddl0r
eww thats ugly, I'll add it to the answer
piddl0r
A: 

An implementation that I use is as follows:

In each of the layout phtml files (default, admin, etc)

I use:

<?= $this->action('header', 'page', 'default') ?>
<?= $this->layout()->content ?>
<?= $this->action('footer', 'page', 'default') ?>

so in my page controller I may have an action called "headerAction" or "adminHeaderAction" which does nothing (or actually could perform specific tasks) but returns a header.phtml (where I can customize my header information for that layout). I can then customize my header and footer separate from the content.

In my controller I just need to set the appropriate layout:

$this->_helper->layout->setLayout('adminLayout');
Arthur Frankel
this is wrong as the action helper fires new request loop. That's slow.
Tomáš Fejfar
Agreed. The action view helper is evil. I wish they'd just remove it already :) http://framework.zend.com/issues/browse/ZF-5840
Richard Nguyen
Good point. I never thought of the extra round trip (for each one).
Arthur Frankel
Hehe all good, everyone's done that kind of stuff before. Your `setLayout()` was spot on though
Richard Nguyen
+1  A: 

If the view helpers are complex use concrete view helper. If they are simple like <h1>my title</h1> use partial view helper to render a template.

Tomáš Fejfar
Do you mind providing an example of at least structure, and example code of the inclusion of partial view helpers using the filenames header.phtml and footer.phtml, or however it works out?
meder
//header<?php echo $this->partial('header.phtml', $optionalArrayWithDataToBeUsedInside);the second parameter like array('title' => 'my page') allows you to use `echo $this->title` inside your partial. For view helper setup see the manual.
Tomáš Fejfar
+1  A: 

Directory structure:

/application
  /layouts
    /scripts
      /layout.phtml
      /profile.phtml
      /admin.phtml
      /_header.phtml
      /_footer.phtml

layout.phtml:

<?php echo $this->doctype('HTML4_STRICT') ?>
<html> 
<head>  
  <title>Bah</title>
</head> 
<body>
  <?php echo $this->render('_header.phtml') ?>
  <?php echo $this->layout()->content ?>
  <?php echo $this->render('_footer.phtml') ?>
</body>
</html>

profile.phtml:

<?php echo $this->doctype('HTML4_STRICT') ?>
<html> 
<head>  
  <title>Profile</title>
</head> 
<body>
  <!-- profile header -->
  <?php echo $this->layout()->content ?>
  <!-- profile footer-->
</body>
</html>

FooController.php:

profileAction()
{
    // do stuff
    $this->_helper->layout->setLayout('profile');
}

This method allows you to change the entire page structure of different layouts (The admin suddenly needs a sidebar!). You trade off some code duplication for enhanced flexibility and maintainability.

If this is not important to you, a view helper would work also (query the Front Controller to find out if the request came from either the admin or profile actions, switch). For my tastes though, that type of logic is too involved to belong in the view.

Richard Nguyen
Awesome, just what I needed - I'll try out the implementation.
meder