Putting It All Together
To put this all together, createDatabase.php
in the
lib
directory, and
loadClass()
will be able to find it. Your
index.php
file should now instantiate
$view
and
$db
and store them in the registry. You can also create a function called
__autoload()
to automatically handle loading all the classes you need:
<?php
include 'Zend.php';
function __autoload($class)
{
Zend::loadClass($class);
}
$db = new Database('/path/to/db.sqlite');
Zend::register('db', $db);
$view = new Zend_View;
$view->setScriptPath('/path/to/views');
Zend::register('view', $view);
$controller = Zend_Controller_Front::getInstance()
->setControllerDirectory('/path/to/controllers')
->dispatch();
?>
views
directory. The
index.php
file can be used for displaying the index view:
<html>
<head>
<title>News</title>
</head>
<body>
<h1>News</h1>
<?php foreach ($this->news as $entry) { ?>
<p>
<a href="/view/<?php echo $this->escape($entry['id']); ?>">
<?php echo $this->escape($entry['title']); ?>
</a>
</p>
<?php } ?>
<h1>Add News</h1>
<form action="/add/news" method="POST">
<p>Title:<br /><input type="text" name="title" /></p>
<p>Content:<br /><textarea name="content"></textarea></p>
<p><input type="submit" value="Add News" /></p>
</form>
</body>
</html>
view.php
template can be used for displaying a specific news entry:
<html>
<head>
<title>
<?php echo $this->escape($this->news['title']); ?>
</title>
</head>
<body>
<h1>
<?php echo $this->escape($this->news['title']); ?>
</h1>
<p>
<?php echo $this->escape($this->news['content']); ?>
</p>
<h1>Comments</h1>
<?php foreach ($this->comments as $comment) { ?>
<p>
<?php echo $this->escape($comment['name']); ?> writes:
</p>
<blockquote>
<?php echo $this->escape($comment['comment']); ?>
</blockquote>
<?php } ?>
<h1>Add a Comment</h1>
<form action="/add/comment" method="POST">
<input type="hidden" name="newsId"
value="<?php echo $this->escape($this->id); ?>" />
<p>Name:<br /><input type="text" name="name" /></p>
<p>Comment:<br /><textarea name="comment"></textarea></p>
<p><input type="submit" value="Add Comment" /></p>
</form>
</body>
</html>
admin.php
template can be used to approve news entries:
<html>
<head>
<title>News Admin</title>
</head>
<body>
<form action="/admin/approve" method="POST">
<?php foreach ($this->news as $entry) { ?>
<p>
<input type="checkbox" name="ids[]"
value="<?php echo $this->escape($entry['id']); ?>" />
<?php echo $this->escape($entry['title']); ?>
<?php echo $this->escape($entry['content']); ?>
</p>
<?php } ?>
<p>
Password:<br /><input type="password" name="password" />
</p>
<p><input type="submit" value="Approve" /></p>
</form>
</body>
</html>
Note: To keep things simple, this form accepts a password as an access control mechanism.
With the templates in place, you just need to replace the comment placeholders in the controllers with a few lines of code. For example,
IndexController.php
becomes the following:
<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
/* List the news. */
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews();
echo $view->render('index.php');
}
public function noRouteAction()
{
$this->_redirect('/');
}
}
?>
AddController.php
is a bit more involved, so it requires more code:
<?php
class AddController extends Zend_Controller_Action
{
function indexAction()
{
$this->_redirect('/');
}
function commentAction()
{
/* Add a comment. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
$name = $filterPost->getAlpha('name');
$comment = $filterPost->noTags('comment');
$newsId = $filterPost->getDigits('newsId');
$db->addComment($name, $comment, $newsId);
$this->_redirect("/view/$newsId");
}
function newsAction()
{
/* Add news. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
$title = $filterPost->noTags('title');
$content = $filterPost->noTags('content');
$db->addNews($title, $content);
$this->_redirect('/');
}
function __call($action, $arguments)
{
$this->_redirect('/');
}
}
?>
AdminController.php
, you handle two actions, displaying the admin interface and approving news:
<?php
class AdminController extends Zend_Controller_Action
{
function indexAction()
{
/* Display admin interface. */
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews('NEW');
echo $view->render('admin.php');
}
function approveAction()
{
/* Approve news. */
$filterPost = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
if ($filterPost->getRaw('password') == 'mypass') {
$db->approveNews($filterPost->getRaw('ids'));
$this->_redirect('/');
} else {
echo 'The password is incorrect.';
}
}
function __call($action, $arguments)
{
$this->_redirect('/');
}
}
?>
ViewController.php
:
<?php
class ViewController extends Zend_Controller_Action
{
function indexAction()
{
$this->_redirect('/');
}
function __call($id, $arguments)
{
/* Display news and comments for $id. */
$id = Zend_Filter::getDigits($id);
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews($id);
$view->comments = $db->getComments($id);
$view->id = $id;
echo $view->render('view.php');
}
}
?>
More Information
This tutorial barely scratches the surface, but there are already a few additional resources available. In addition to the manual at http://framework.zend.com/manual/, Rob Allen has provided some details about his experiences with the Zend Framework at http://akrabat.com/zend-framework/, and Richard Thomas has provided some useful notes at http://www.cyberlot.net/zendframenotes. If you have some commentary of your own, please visit the new Zend Framework forum at http://www.phparch.com/discuss/index.php/f/289//.Closing Note
It's easy to judge a preview release harshly, and I encountered a few frustrations of my own while writing this tutorial. Overall, I think the Zend Framework shows a lot of promise, and everyone involved is working hard to continue to improve it.
Note: Comments? Thoughts? Questions? Check out our new Zend Framework forum at
http://www.phparch.com/discuss/index.php/f/289//.