A Practical Tutorial Of Zend Framework(六)

Putting It All Together

To put this all together, create Database.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();

?>
Next, create some simple templates in the 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>
The 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>
Finally, the 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('/');
    }
}

?>
With everything organized, the entire business logic for the front page of this application is reduced to four lines of code. 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('/');
    }
}

?>
Because the user is redirected after submitting a form, no view is necessary in this controller. In 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('/');
    }
}

?>
Finally, 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');
    }
}

?>
This provides a fully-functional, albeit very simplistic, application for sharing news and posting comments. The best part is that adding more features is easy due to the elegant design, and as the Zend Framework continues to mature, things should only get better.

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//.
About the author—Chris Shiflett is the principal of Brain Bulb, a consulting firm that specializes in PHP development and security. Chris' blog is at http://shiflett.org.

转载于:https://my.oschina.net/elinac/blog/698029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值