So I have gone head first in using Zend framework. First order of business has been to port libraries/features to zend framework. One of the features that I have always migrated is CakePHP’s view layout structure. For CI I was fortunate enough to get a library already made but no such luck this time.
Installation:
- Download my very own library Ak33m Library (this will contain all the libs I have made to enhance my zend framework experience).
- Extract the library to the location where you zend framework library is located. Sample layout:
library (folder containing all libraries) -> Zend(zend framework)
library (folder containing all libraries) -> Ak33m (my library)
- Add some lines to your bootstrap (for most people index.php. NB the library folder containing the framework files and my library should set to the include path at the top of the bootstrap file) to make the cake view renderer your default view renderer. (NB I am only doing it in the bootstrap because this is the most convenient place)
[php]
//Get the view renderer helper that injects the view object in each controller
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(”viewRenderer”);
//replace the default view object (Zend_View) with the cake view object provided in the Ak33m library (Cake_View)
$viewRenderer->setView(new Cake_View());
//This is optional, but if a full CakePHP implementation is desired change the view extension for view files to ‘thtml’
$viewRenderer->setViewSuffix(”thtml”);
[/php]
- In the application->views folder create two subfolders (there are methods to set custom paths for layouts and elements these are just the default paths):
- layouts (application->views->layouts) - This folder is where the view layouts will be stored.
- elements (application->view->elements) - This folder is where the elements for a view will be stored
Usage:
The Cake_View object is a cross between Zend_View and CakePHP’s View object. You can use Cake_View just as you would Zend_View (see the zend documentation for details of view integration in zend) and to make use of the Cake aspect before you render your view simply set the layout that your view should be rendered in. Below is a sample IndexController.php that illustrates the most basic usage:
[php]
require_once ‘Zend/Controller/Action.php’;
class IndexController extends Zend_Controller_Action
{
/**
* The default action - show the home page
*/
public function indexAction()
{
$this->view->setLayout(”2column”);
}
}
[/php]
Next order of business is to create the 2column layout. A layout is a re-usable presentation that wraps around a specific view. In this example a 2column.thtml file is created in the default application->views->layouts folder. The code that goes in that file is here:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”><head>
<title>Cake Rendering Example</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<style type=”text/css”>
* {
margin: 0;
padding: 0;
}
body {
font-size: 62.5%;
font-family: Garamond, sans-serif;
}
h2 { font-size: 4.0em; }
p, li,h1 { font-size: 1.8em; }
.clear { clear: both; }
#header {
background: #;
padding: 15px 0 15px 15px;
}
#content {
padding: 15px 30px 0 0;
}
#main-content {
margin-left: 175px;
max-width: 600px;
}
#sidebar {
width: 145px;
float: left;
padding: 0 0 0 15px;
}
#sidebar ul {
list-style: inside;
}
</style>
</head>
<body>
<div id=”header”>
<h2>Zend Framework Cake View Rendering Example</h2>
</div>
<div id=”content”>
<div id=”sidebar”>
<?php //renders the sidebar elements with this items array to populate the menu items. NB $this->element(”sidebar”,$somedata) could have been used?>
<?= $this->renderElement(”sidebar”,array(”items”=>array(”home”,”aboutme”))) ?>
</div>
<div id=”main-content”>
<?php //$content for layout is where the view’s content will be rendered?>
<?= $this->content_for_layout ?>
</div>
<div class=”clear”></div>
</div>
</body>
</html>
In this layout an element called “sidebar” is rendered with an array paased to it (an stdClass object can also be passed as well).
[php]
renderElement(”sidebar”,array(”items”=>array(”home”,”aboutme”))) ?>
[/php]
For this element to be rendered a file named sidebar.thtml should be placed in application->views->elements. The code for sidebar.html is:
<h1>Side Bar Element</h1>
<ul>
<?php foreach ($this->items as $item):?>
<li><?=$item?></li>
<?php endforeach;?>
</ul>
The second parameter of renderElement is the data that is passed to the element. The array keys(or properties if an object was passed) of the second parameter are made into variables of the element view. So in our example the array value for “items” can be accessed within the element using
[php]
$this->items
[/php]
Because it is a tutorial it may seem harder than it really is but for the benefit of those following along the files used can be found here. For more information on how the Cake View works check out the original documentation here
For people who are using Flex Builder 3, and have the “Illegal Override of XMLRPCObject” error, you need to:
1) import the project into flex builder 3
2) it’ll tell you that the project was made in previous version of flex builder. Select the “default” version (which is Flex 3)
3) Open up XMLObject.as, look for “setCredentials” and replace the function signature with: override public function setCredentials (username:String,password:String,charset:String = null):void
4) right below, find “setRemoteCredentials” and replace the function signature with: override public function setRemoteCredentials (username:String,password:String,charset:String = null):void
5) build it (automatically) and enjoy!