Zend Framework 1.11 Layout classes etc

622 views Asked by At

just started using Zend Framework and I'm wondering what to do with odd classes I have. I'm pretty much wondering about best practise for getting everything setup.

I've just enabled layout and I have the wrapping html head, body, footer etc.

In my previous application I created a bundler script where you pass a list of CSS files and JS scripts in a particular order. If it's in development mode then it'll just loop through the filenames and print them out like:

foreach ($CSS_Scripts as $file) {
    echo '<link src="' . $file . '" ref="stylesheet" />';
}

foreach ($JS_Scripts as $file) {
    echo '<script type="text/javascript" src="' . $file . '"></script>';
}

If it's development mode then it may print out 10 individual CSS and JS files. If it's in production then it will bundle them up so theres only 1 CSS and 1 JS file.

With this being within templating... I'm not quite sure where the logic is supposed to go. It's logic that should exist on every page so it's inside layout.phtml but where am I meant to put my classes and how am I meant to run them? Do I put them in projectname/library and run the bundler logic inside Bootstrap.php then assign them to a view and call a view inside layout.phtml?

Also... say I want a login form to be available at the top of every page when logged out inside layout.phtml I'm assuming. I've created the Form Application_Form_Login but to allow logins I need to have an action within a Controller? Should I be playing the Login action within the Bootstrap as well so that its possible to login from every page and I'm not duplicating loginAction on every page?

But then again theres also Zend_Auth which would provide more flexibility right? Maybe this is initiated inside Bootstrap and then throughout layout.phtml and any View I can just do conditionals for if you're logged in?

And for landing pages where you have SEO tags in the URL... I'm guessing its a case of creating a seperate Controller/View per keyword like (just examples)... stack-overflow/ computer-questions/ digital-clocks/ and then the content inside these Views

Thanks... I'm quite confused as its a different way of thinking when everything is seperated :D I'm used to having full control everywhere and doing what I want and where I want but that's lead to spaghetti code -.-

1

There are 1 answers

3
drew010 On BEST ANSWER

Here are some answers to a few of your questions...

The Bootstrap is for setting up your application and resources. You really don't want any application specific code in your bootstrap. Use the bootstrap to set up your views, register plugins, make sure database and resources like that are set up, possibly initialize your session in the bootstrap if absolutely necessary.

Your bundling process should be done in the layout.phtml file. If you properly utilize some of the placeholder helpers Zend Framework offers, you can easily accomplish what you want.

Here is an example, this code would be in your layout.phtml file:

<?php
$this->headScript()
     ->prependFile($this->baseUrl('js/importantCode.js'), 'text/javascript');

echo $this->headScript();

Using the headScript helper, we add the file 'js/importantCode.js' to the list of scripts to be included in the layout. By doing this in the layout, it guarantees that the importantCode.js file is always included in your pages. Now say you have a special page that uses some additional javascript you don't use everywhere, in your view script, you can simply do this:

<?php
$this->headScript()->appendFile($this->baseUrl('js/calendar.js'))
                   ->appendFile($this->baseUrl('js/tooltips.js'));
?>

This will add two more javascript files to your tags, but only for this particular view. If you find yourself repeating this logic for a lot of pages, you may be able to use a plugin or a view helper to simplify adding these files to your views, but they key is you do not need the bootstrap to do it. Similar helpers exist for including CSS or inline styles as well.

A common place to put some of your own custom classes is in the library/My folder. Then you can follow the Zend structure for classes and directories, and name your classes like My_Custom_Class (library/My/Custom/Class.php) and you can autoload them as required.

From any of your Zend Application code, you can simply say $obj = new My_Custom_Class() and Zend will autoload that class from library/My/Custom/Class.php so you don't have to include or require it everywhere. By instantiating it only when needed, it saves having to include it all the time when it is not needed.

For your login process, I would create a single action to handle the login. It could be in a dedicated controller, or you could find an appropriate controller to include the login process in (such as IndexController for example). You could use a View Partial to include in your layout for displaying the login form, it would always point to your login action to process the login, and you could use a hidden value to redirect the user to the controller and action they logged in from if you wish.

As for your SEO urls, custom routes may be useful in your situation, or you could just structure your URL's so most of the SEO are just URL parameters. Take the URL http://yoursite.com/product/view/category/Digital-Clocks/cid/1234. Product is the controller, view is the action, the param category is Digital-Clocks, and the cid (category id) is 1234. You could achieve shorter and more friendly URLs with custom routes.

Hope that helps some.