By
Apex Tg India Pvt Ltd
http://www.apextgi.in
View
View
Scripts (templates)
PHP-based script templates to present data
Should contain only display logic, not business logic
Default naming: myaction.phtml
Helpers
Classes and methods that provide reusable view functionality
Examples of built in view helpers: escape(), formText(), partial(), partialLoop(), headTitle()
Write your own, too
Output filters
Layout
Placeholders
Controller (again)leads to view
View script automatically rendered
Zend_Layout
Zend_Layout
Two-step view pattern
Uses Zend_View for rendering
Placeholders useful for setting javascript, titles, other variable data
Layout view helper
shortcut to layout placeholder
These are equivalent:
//fetch'content'keyusinglayouthelper:
echo$this->layout()->content;
//fetchcontent'keyusingplaceholderhelper:
echo$this->placeholder('Zend_Layout')->content;
Enable in bootstrap
// accept default path
Zend_Layout::startMvc();
// or specify path
$layoutPath = realpath(dirname(__FILE__) .
'/../application/layouts');
Zend_Layout::startMvc(array("layoutPath => $layoutPath));
My own view helper: TitleCase.php
<?php
Require_once 'Zend/View/Interface.php';
/**
* TitleCase helper
*/
class Zend_View_Helper_Title_Case {
public $view;
public function titleCase($string = '')
{
// convert incoming string so that
// first letter of each word is capitalized/
// but all other letters are lowercase.
// Also trim the string.
return ucwords(strtolower(trim($string)));
} //(public function titleCase())
public function setView(Zend_View_Interface $view) {
$this->view = $view;
Usage:
echo $this->titleCase(mozilla
firefox);
// Mozilla Firefox
Model
Model
Models are abstract representations of data
Can be extended from:
Zend_Db_Table_Row For database abstraction
Zend_Feed_Element For RSS abstraction
Yahoo_Result For Yahoo abstraction
Or any other class that fits your needs
Or build your own own abstract representations of
your data
Models (more)
Model classes contain business logic to prepare
complex data for presentation
I stuff any weird code in models so that
controllers/views are clean
Model: example
<?php
// model: Busyflag.php
class Busyflag
{
// The table name
protected $name = SYSFLAGS'; // old-fashioned System 36 table
// isSiteUp: return true if up, false if down
public function isSiteUp() {
// one record, with key "B"
$sql = "select BZYFLG from {$this->name} where RECID = 'B'";
$row = SBSDbhelp::getOneRow($sql);
// true if Y, false otherwise.
return $row['BZYFLG'] == 'Y';
} //(public function isSiteUp())
} //(class Busyflag)
// usage (from a preDispatch front controller plugin)
$busyFlag = new Busyflag();
if (!$busyFlag->isSiteUp()) {
// Take user to "site down" page.
$request->setControllerName("Down");
$request->setActionName("index");
} //(if (!$busyFlag->isSiteUp()))
Controller + Model + View: traditional
<?php
// Controllers/SearchController.php
require_once 'Zend/Controller/Action.php';
class SearchController extends Zend_Controller_Action
{
public function indexAction()
{
$prodid = $this->_getParam('prodid');
// look up product data in model
$products = new Search_Products();
$results = $products->find($prodid);
// assign results to view
$this->view->results = $results;
}
}
<?php
// views/scripts/search/index.phtml
?>
Products found:<ul>
<?php
foreach ($this->results as $result) {
echo "<li>{$this->escape($result->prodname)}</b>{$this->escape($result>available)}</li>";
}
?>
</ul>
MVC your way: custom routing rules
Can declare custom routing rules with
Zend_Controller_Router_Route classes
Not limited to controller/action/param format
Example: year/month/day/title as in a blog posting:
URL: http://myblog.com/2008/02/24/i-like-routers
$route=newZend_Controller_Router_Route(
':year/:month/:day/:title',
array(
'year'=>2009,
'controller'=>articles',
'action'=>'show'
),
array(
'year' =>'\d+,
month'=>'\d+,
day =>'\d+,
)
);
$router->addRoute(posts',$route);
MVC your way: multi-MVC
Can separate app sections into modules (Mitch P.s
atomic MVC)
$front->setControllerDirectory(array(
'default' => '/demo/application/controllers',
'blog' => '/demo/application/blog/controllers' ));
Blog_IndexController is class name of index action
controller within blog app/module
URL: example.com/blog or /blog/index or
/blog/index/index
IndexController is still class name within default
app/module
URL: / or /index or /index/index
Tools can help get you started
Zend Studio for Eclipse creates default directory structures and classes
for you
Free trial: http://www.zend.com/en/products/studio/
Questions?
Send me your thoughts: [email protected]
See you at APEX
TG!