views:

2075

answers:

7

I was recently brought on to help with a project that was made up of individual HTML files with the exception of a PHP contact form. So there's not even a hint of object oriented programming, MVC, or layouts (or even PHP for that matter).

The project is quite large, but I wanted to slowly integrate the Zend Framework into this project, mostly to start using layouts. There are so many redundancies that it is such a waste of time to make small updates that should have been made in one file.

In the early days of PHP, you could separate your content blocks by including them in each page (a header and footer for example). Now, using MVC frameworks like the Zend Framework, you can create layout files that include the individual page content (views) using a view helper. I really like this because it means I only have to "include" my header, or footer, in one place.

However, I'm not sure how this would work without dispatching/bootstrapping the application (i.e. using the Zend Framework MVC components as standalone components instead). What would be the best approach to switching the site over to use layouts? How would it work? Is this even a good idea?

+2  A: 

Personally, I'd recommend not using the Zend Framework if you're only planning on using it for template/layout management. There are much better solutions for templating with PHP, with Smarty being the obvious choice (formerly part of the PHP project).

Smarty does provide a reasonably easy integration with the Zend Framework, if you need to do this at a later date, and there are a few articles on it on the Zend DevZone.

James Burgess
i wouldn't recommend using smarty, for me its an extra layer of computing that is not necessary. I have found my self thinking of various hacks for smarty for things that needed 2-3 lines in php, just because those things werent suported in smarty syntax.
solomongaby
Your opinion differs, so it's worth a downvote? Wow. I thought the caption said: "this answer is not helpful."
James Burgess
Interesting to see you've also recommended integration with Smarty...
James Burgess
yeah i thought that was "interesting" too. That's not what a downvote is for.
Andrew
A: 

i dont think you can do this by default, i think you need to use the Zend View as well. But i am sure you can modify the Zend_Layout to work with your existing setup. Sorry i cant be more specific on how to do this, because it depends a lot of what is currently in place.

As a starting point i recommend looking into Smarty integrations into Zend to see how you can modify it.

solomongaby
And in your other comment you do not recommend Smarty?
sims
A: 

I'm not sure if this is what you're looking for, but the bbc has some videos where they use parts of the zend framework without using the whole MVC. I think they are in parts 4 & 5, http://bbcwebdevelopers.blip.tv/

+4  A: 

You can certainly use Zend_Layout in isolation and it's detailed in the manual:

28.2.3. Using Zend_Layout as a Standalone Component

I believe you would then need to capture you script's output via output buffering, and pass it to the layout to be echoed.

This situation is not ideal, and without moving to the full MVC I'd probably recommend using a different, basic templating system or following the advice in other comments here.

David Caunt
A: 

Zend_Layout isn't really about including a header or footer, rather it is best used to insert various bits of content into a single large template. It's pretty sophisticated and as well as common use cases such as inserting the main body of content you can insert HEAD tags such as CSS, scripts or title tags and custom placeholders such as navigation.

From your description I'd go for a simple PHP include header and footer solution. It will be easier to implement and will remove any duplication of common template elements.

If you really want to use this to learn Zend Framework you could use Zend_Layout in the standalone mode as dcaunt suggests. There is no point trying to use the MVC bootstrap system since your site doesn't appear to need it. I'd simply do something like use URL rewriting to send all *.html requests to a single PHP file which then starts Zend_Layout and loads the requested file into the $layout->content variable via something like file_get_contents()

You could then also use ZF for the form processing if you wished.

simonrjones
+4  A: 

I've recently begun a transformation of a mish-mash file structure* to make use of ZF's layout & views. The goal is to move all files containing html into the recommended file structure for layouts and views. This is how to prepare for it:

  1. Extract all markup from each file, keep variables in it but no logic. Name this file /application/views/scripts/page/view.phtml (for example /application/views/scripts/index/login.phtml)
  2. Create a skeleton that fits most pages as /application/layouts/scripts/layout.phtml and let it contain something like this:

    <?php echo $this->doctype('XHTML1_STRICT'); ?>
    <html>
    <head>
        <?php echo $this->headLink(); ?>
    </head>
    <body>
        <div id="wrapper">
        <?php echo $this->layout()->content; ?>
        </div>
    </body>
    </html>
    
  3. (Add Zend to your include path and register its Autoloader.) Create a file that will be included in all of your files (unless you have a single point of entry, in that case - place it there!) Create a reference to your layout, equivalent to this:

    $view = new Zend_View();
    $view->setScriptPath('/application/views/scripts');
    
    
    $layout = new Zend_Layout();
    $layout->setLayoutPath('/application/layouts/scripts');
    $layout->setView($view);
    
    
    Zend_Registry::getInstance()->set('Zend_Layout', $layout);
    
  4. Each php file you have left (with logic, not html) needs to have access to the layout, and modifying it:

    $layout = Zend_Registry::getInstance()->get('layout');
    
    
    $view = $layout->getView();
    $view->headLink()->appendStylesheet('/css/a_css_file.css');
    
    
    // most important step, done automatically when using MVC, but now you have to do it manually
    $layout->content = $view->render('index/login.phtml');
    
    
    echo $layout->render();
    
  5. Most important next step is adding another layout:

    // /application/layouts/scripts/blank.phtml
    <?php echo $this->doctype('XHTML1_STRICT'); ?>
    <html>
        <head></head>
        <body>
            <?php echo $this->layout()->content; ?>
        </body>
    </html>
    

    and in one of your logic-containg files

    $layout = Zend_Registry::getInstance()->get('layout');
    $layout->setLayout('blank');
    

Ideally, in a near future you can start adapting to a MVC-like structure by looking at a design pattern like Front Controller.

*One file: model, controller, html, in no logical order.

chelmertz
A: 

@chelmertz, is the way to go, This is exactly how I have it set up.

rettal
Should be a comment...
sims