際際滷

際際滷Share a Scribd company logo
Zend Framework 亳 MVC27 仄舒舒 2010 亞.弌舒仆从-亠亠弍亞亠舒仍亳亰舒亳 仂亟亠仍亳
仂从仍舒亟亳从弌亠亠仆从仂 仍亠从舒仆亟亠亟亳亶 舒亰舒弍仂亳从 从仂仄仗舒仆亳亳 Norada仂仍亠亠 7-亳 仍亠 舒亰舒弍仂从亳 亳 仗仂亠从亳仂于舒仆亳 仗仂亞舒仄仄仆仂亞仂 仂弍亠仗亠亠仆亳仂仍亠亠 2- 仍亠 亳仗仂仍亰仂于舒仆亳 Zend Framework
弌仂亟亠亢舒仆亳亠 亟仂从仍舒亟舒丼仂 舒从仂亠 MVC?MVC 于 Zend Framework亠舒仍亳亰舒亳 仄仂亟亠仍亳
丼仂 舒从仂亠 MVC?
Model View Controller仂亟亠仍 (Model)仂于亠舒亠 亰舒 亟舒仆仆亠 亳 仗仂于亠亟亠仆亳亠 弍亳亰仆亠 仍仂亞亳从亳 仗亳仍仂亢亠仆亳亠亟舒于仍亠仆亳亠 (View)- 仂于亠舒亠 亰舒 仍仂亞亳从 于磶舒仆仆  仂仂弍舒亢亠仆亳亠仄 亳仆仂仄舒亳亳仂仆仂仍仍亠 (Controller)仂弍舒弍舒于舒亠 仗仂仍亰仂于舒亠仍从亳亶 于于仂亟
仂仗亠亳亠 仄仂亟亠仍 亟仍 亳亰仄亠仆亠仆亳 亠 仂仂礌亳 亳 仗仂仍亠仆亳 仆亠仂弍仂亟亳仄 亟舒仆仆
仗亠亠亟舒亠 亟舒仆仆亠 仗亠亟舒于仍亠仆亳ミ}亠仄舒 仂仆仂亠仆亳亶 于 MVC
MVC 于 Zend Framework
Zend_Controller_Front丿舒弍仍仂仆 仗仂亠从亳仂于舒仆亳 个仂仆 仂仆仂仍仍亠 (Front Controller)亊于仍磳 舒从 仆舒亰于舒亠仄仄 束亟亳仆仂从仂亶損 (Singleton)丶亠仆舒仍亳亰仂于舒仆仆舒 仂从舒 于仂亟舒 亟仍 于亠 亰舒仗仂仂于亠仍亠亞亳仂于舒仆亳亠 亰舒仗仂舒 从 仗亠亳舒仍亳亰亳仂于舒仆仆仂仄 仂弍舒弍仂亳从 (Zend_Controller_Action)
仆亳亳舒仍亳亰舒亳 仗亳仍仂亢亠仆亳磇舒亶仍 /public/index.php<?phpZend_Controller_Front::run('../application/controllers');
亳仗亠亠亳亰舒亳Zend_Controller_Front 从仂仂亟亳仆亳亠 于亠 仗仂亠 亢亳亰仆亳 仗亳仍仂亢亠仆亳弍亠从 亰舒仗仂舒 (Request)-  POST, GET, SESSION, COOKIES舒亳亰舒仂 (Router)亳仗亠亠 (Dispatcher)弍亠从 仂于亠舒 (Response)- 弍亠亳亰舒亳 于于仂亟舒
Zend_Controller_Action弍舒从仆亶 从仍舒 亟仍 亠舒仍亳亰舒亳亳 MVC 于 Zend Framework舒仍亠亟仂于舒仆亳亠 Zend_Controller_Action从仍舒舒仄亳 - 从仂仆仂仍仍亠舒仄亳仍舒  从仂仆仂仍仍亠 亞仗仗亳亠仆舒弍仂 仂弍舒弍仂亳从仂于 于 于亳亟亠 仗弍仍亳仆 仄亠仂亟仂于  亟亠亶于亳亶.
Zend_Controller_Action个舒亶仍 ../application/controllers/GuestBookController.php<?phpclass GuestBookControllerextends Zend_Controller_Action{public function listCommentsAction()    {//[...]    }public function addCommentAction()    {//[...]    }}
Zend_View亠舒仍亳亰舒亳 亠亟舒于仍亠仆亳 于 Zend Framework仆亠仗亠舒亳 束从亳仗仂于 于亳亟舒損仂亟亟亠亢从舒 仍ミ英 仂弍舒弍仂亳从仂于 舒弍仍仂仆仂于public function listAction(){$guestBookModel= new GuestBook();$comments = $guestBookModel->getComments();$view = new Zend_View();$view->comments = $comments;echo $view->render('guest-book.php');}
弌从亳仗 于亳亟舒 guest-book.php个舒亶仍 ../application/views/scripts/guest-book.php<html><head><title>Comments</title></head><body><div>Comments:</div><?phpforeach($this->comments as $comment): ?><div class = "comment"><?phpecho $this->trim($comment); ?></div><?phpendforeach; ?></body></html>
仂仄仂仆亳从亳 于亳亟舒亠舒仆亳亰仄 亟仍 舒亳亠仆亳 仆从亳仂仆舒仍仆仂亳 亳亟舒仗仂于仂磳仄舒 仍仂亞亳从舒 仗亠亟舒于仍亠仆亳
仂仄舒亳仂于舒仆仆亳亠 亟舒仆仆
亟仂仗 从 仂亟亠仍亳个舒亶仍 ../application/views/helpers/Trim.php<?phpclass My_View_Helper_Trim{public function trim($value)    {return trim($value);    }}
仂仄仂仆亳从 亟亠亶于亳 ViewRenderer仂仄仂仆亳从亳 亟亠亶于亳:弌仗仂仂弍 舒亳亳 仆从亳仂仆舒仍仆仂 从仂仆仂仍仍亠仂于
亠亠于舒亳从亳 仂弍亳亶于仂仄舒亳亰舒亳 仆舒仂亶从亳 仂弍亠从舒 亳亟舒于仂仄舒亳亰舒亳 亠仆亟亠亳仆亞舒 舒弍仍仂仆仂于
仂仄仂仆亳从 亟亠亶于亳 ViewRenderer个舒亶仍 ../application/controllers/GuestBookController.phpclass GuestBookControllerextends Zend_Controller_Action{public function listComments()    {$guestBookModel= new GuestBook();$comments = $guestBookModel->getComments();$this->view->comments = $comments;    } }个舒亶仍 ../application/views/scripts/guest-book/list.phtml<?phpforeach($this->comments as $comment): ?><div class=comment><?phpecho $comment ?></div><?phpendforeach; ?>
仂亟亠仍
Zend_Model ?Zend Framework 仗亠亟仂舒于仍磳 仆舒弍仂 于仗仂仄仂亞舒亠仍仆 从仂仄仗仂仆亠仆 亟仍 亠舒仍亳亰舒亳亳 仄仂亟亠仍亳仂亟亠仍  亠亰仍舒 仗仂亠舒 仄仂亟亠仍亳仂于舒仆亳 仗亳从仍舒亟仆仂亶 亳亠仄
束丐仂仆从舒晛 仄仂亟亠仍仂仆仂仍仍亠 于仗仂仍仆磳 于亠 仆从亳亳 仄仂亟亠仍亳弍亳亰仆亠 仍仂亞亳从舒
亠仆亳亠 亳 亰舒仗亳 亟舒仆仆
仗仂于亠从舒 于仂亟亠亶 亳仆仂仄舒亳亳仂仍 仄仂亟亠仍亳 亰舒从仍ム舒亠 于 亟仂仗亠 从 亟舒仆仆仄
束丐仂仆从舒晛 仄仂亟亠仍个舒亶仍 ../application/models/GuestBook.php<?phpclass GuestBookextends Zend_Db_Table_Abstract{protected $_name = "comments";}
束丐仂仍亶損 从仂仆仂仍仍亠class CommentsControllerextends Zend_Controller_Action{public function listComments()    {$postId= $this->_request->getParam("postid");$isUser= $this->_request->getCookie("user");$guestBook= new GuestBook();$rows = $guestBook->fetchAll("post_id = ?", $postId);$commentsNumber= count($rows);$comments = array();$hiddenComments= array();foreach($rows as $row) {if ($row["visible"] == 1) {$comments[] = $row["text"];            } else {if ($isUser) {$hiddenComments[] = $row["text"];                 }            }        }$this->view->comments = $comments;$this->view->hiddenComments = $hiddenComments;    }}
束丐仂仍舒晛 仄仂亟亠仍个舒亶仍 ../application/models/GuestBook.php<?phpclass GuestBook{protected $dbTableGateway;public function getVisibleCommentsByPostId()    {    }public function getHiddenCommentsByPostId()    {    }}
束丐仂仆从亳亶損 从仂仆仂仍仍亠public function listComments(){$postId= $this->_request->getParam("post");$isUser= $this->_request->getCookie("user");$guestBook= new GuestBook();$comments = $guestBook->getVisibleCommentsByPostId($postId, $isUser);$hiddenComments= $guestBook->getHiddenCommentsByPostId($postId, $isUser);$this->view->comments = $comments;$this->view->hiddenComments = $hiddenComments;}
弌仍仂亳 (Layers)
弌亠于亳仆亶 仍仂亶丿舒弍仍仂仆 仗仂亠从亳仂于舒仆亳 个舒舒亟弌从于舒亠 亠舒仍亳亰舒亳 仂弍亠从仂于 弍亳亰仆亠 仍仂亞亳从亳 亳 亳 于亰舒亳仄仂亟亠亶于亳亠-  舒仍亳亟舒亳 仗仂仍亰仂于舒亠仍从仂亞仂 于于仂亟舒于仂亳亰舒亳 亳 仗仂于亠从舒 仗舒于 亟仂仗舒GuestBookService个舒亶仍 ../application/models/GuestBookService.php<?phpclass GuestBookService{//[...]public function addComment($commentData)    {$validator= $this->getCommentValidator();if ($validator->isValid($commentData)) {$this->getCommentTable()->insert($commentData);           }    }}
仂仄亠仆仆舒 仄仂亟亠仍弌仍仂亶 亳仆从舒仗仍亳ム亳亶 仂弍亠从 弍亳亰仆亠 仍仂亞亳从亳 亳 于亰舒亳仄仂亟亠亶于亳亠 仄亠亢亟 仆亳仄亳亟亠仍磳 弍亳亰仆亠 仍仂亞亳从 亳亠仄 仂 亟仂仗舒 从 亟舒仆仆仄 亳 亠于亳仆仂亞仂 仍仂
仂仍亰仂于舒亠仍 (User)个舒亶仍 ../application/models/User.php<?phpclass User{protected $_name;protected $_comments;public function isAdmin(){/*[...]*/}public function authenticate(){/*[...]*/}public function getName(){/*[...]*/}public function getComments(){/*[...]*/}}
仂仄仄亠仆舒亳亶 (Comment)个舒亶仍 ../application/models/Comment.php<?phpclass Comment{protected $_text;/**      * Returns comments author      *      * @return User      */public function getAuthor()    {return $this->_user;    }}
弌仍仂亶 亟仂仗舒 从 亟舒仆仆仄亠仍亳仂仆仆亠 弍舒亰 亟舒仆仆Zend_Db个舒亶仍仂于舒 亳亠仄舒 亠弍 亠于亳Zend_Feed
Zend_Service亠- Zend_Cache
Table Data Gateway<?php$usersGateway= new UsersGateway();// 仂弍舒于仍亠仆亳亠 仆仂于仂亞仂 仗仂仍亰仂于舒亠仍$usersGateway->insert($userData);// 弍仆仂于仍亠仆亳亠 亟舒仆仆 仗仂仍亰仂于舒亠仍$usersGateway->update($userId, $userData);// 丕亟舒仍亠仆亳亠 仗仂仍亰仂于舒亠仍$usersGateway->delete($userId);// 丼亠仆亳亠 亟舒仆仆$rows = $usersGateway->findAllByAddress($address);// 亠仂弍舒亰仂于舒仆亳亠 于 亟仂仄亠仆仆亠 仂弍亠从$users = array();foreach($rows as $row) {$users[] = new User($row);}
Zend_Db_Table<?phpclass UsersGatewayextends Zend_Db_Table_Abstract{protected $_name = 'users';public function findAllByAddress($address)    {$where = $this->getAdapter()                      ->quoteInto('adress = ?', $address);$rows = $this->fetchAll($where);return $rows;    }}
Active Record / ORM// 仂弍舒于仍亠仆亳亠 仆仂于仂亞仂 仗仂仍亰仂于舒亠仍$user = new User($userData);$user->save();// 弍仆仂于仍亠仆亳亠 亟舒仆仆$user->setName($newUserName);$user->save();// 丕亟舒仍亠仆亳亠 亰舒仗亳亳 仂 仗仂仍亰仂于舒亠仍亠$user->delete();// 丼亠仆亳亠 亟舒仆仆$users = $userFinder->findAllByAddress($address);// 亳仍亳$users = User::findAllByAddress($address);

More Related Content

ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 1)