Zend Framework 1.8 workshop
-
Upload
nick-belhomme -
Category
Technology
-
view
114 -
download
0
description
Transcript of Zend Framework 1.8 workshop
Zend Framework Presentation
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Zend Framework 1.8 workshop
17 June 2009
About Me:PHP5 Zend Certified Engineer (PHP5 ZCE)Zend Framework Certified Engineer (ZCE-ZF)ZF Contributor since 2008Freelance ConsultantPHP Community active member
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework
• Full Stack Framework• Component Library
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
How to Get It?
• Grab it from the download page:http://framework.zend.com/download/latest
• Through Subversion:http://framework.zend.com/svn/framework/standard/tr unk/
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
How To Install?
Add it to your php.ini include_path directive
include_path = .:/usr/local/lib/php:/PathToZendFrame workLibrary
That’s it!
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Starting A New Project With Zend_Tool
First requirements:• For *nix environments copy the zf.sh and zf.php fro m the ZF/bin
To the same directory as your PHP binary
• For Windows: •copy the zf.php and zf.bat to the same directory as your
PHP binary•add the php path to your PATH environment Var:
# Right-click My Computer and select |Properties.# Select the Environment page.# In the System Variables area, click on PATH.# Add the php path to the end in the format VALUE1;V ALUE2;c:\php# Click Apply.# The changes take effect immediately.
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Creating the project “MyProject”
$ zf create project /path/to/non-existent-dir-called -MyProject
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Adding a “MyProject” vhost
<VirtualHost *:80>ServerName MyProjectDocumentRoot absolute-path-to-MyProject-folderErrorLog logs\MyProject-error.logCustomLog logs\MyProject-access.log common
<Directory />Options FollowSymLinksAllowOverride AllOrder allow,denyAllow from allSatisfy all
</Directory></VirtualHost>
Testing environment httpd.conf or httpd-vhosts.conf :
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
127.0.0.1 MyProject
For windows: Add the following line to the /etc/hos t file
Restart apache, fire up the browser and go to: http ://MyProject
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Go to the MyProject directory and issue the followi ng command to create the helloAction in the IndexController an d corresponding view:
$ zf create action hello index
Creating an Action + View
For a full list of commands simply visit:http://framework.zend.com/manual/en/zend.tool.proje ct.create-a-project.html
Or issue the command:
$ zf --help
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
$ zf create module admin
Creating a Module and Controller
$ zf create controller index 1 admin
Note: Trying to create a controller that already ex ists in anotherModule might result in an error. Depending on the Z end_ToolVerion you have. The solution is to apply the patch from ZF Issue Tracker: http://framework.zend.com/issues/browse/ZF-6853
Note: If you applied this patch, make sure all cont roller names in the adminmodule start with Admin_
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Registering the Module
class Bootstrap extends Zend_Application_Bootstrap_ Bootstrap{
protected function _initAutoloadModuleAdmin(){
$autoloader = new Zend_Application_Module_Autoloade r(array('namespace' => 'Admin','basePath' => APPLICATION_PATH.'/modules/admin',
));return $autoloader;
}}
The method is called automatically by Zend_Application as it starts with _init. You can choose the remainder of the method name as you like.
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Registering the Module
resources.frontController.moduleDirectory = APPLICAT ION_PATH "/modules"
myProject/application/configs/application.ini
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Setting up the Mysql Database
Add to configs/application.ini [production]===============================
resources.db.adapter = PDO_MYSQLresources.db.params.host = localhostresources.db.params.username = nickresources.db.params.password = 12345resources.db.params.dbname = myproject
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Create the database table
CREATE TABLE users (id int(11) NOT NULL auto_increment,username varchar(100) NOT NULL,pwd varchar(32) NOT NULL,firstname varchar(20) NOT NULL,PRIMARY KEY (id));
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Populate the table
INSERT INTO users (username, pwd, firstname)VALUES('NickBelhomme', md5('hello'), 'Nick'),('ChanieNavez', md5('strong'), 'Chantal'),('ElvisP', md5('presley'), 'Elvis');
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Model + DAO
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
myProject/application/modules/admin/models/dao/Inte rface.php
interface Admin_Model_Dao_Interface{
public function find($id);public function insert($username, $pwd, $firstname);public function update($id, $username, $pwd, $firstname);public function delete($id);public function findAll();
}
Common interface between all DAOs.
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
myProject/application/modules/admin/models/dao/User DB.php
class Admin_Model_Dao_UserDB extends Zend_Db_Table
implements Admin_Model_Dao_Interface{
protected $_name = 'users';public function find($id){
$id = (int)$id;$row = $this->fetchRow('id = ' . $id);if (!$row) {
throw new Exception("Count not find row $id");}return $row;
}public function insert($username, $pwd, $firstname){
$data = array('username' => $username,'pwd' => $this->hashPassword($pwd),'firstname' => $firstname,
);parent::insert($data);
}//...
}
Very Simple DAO(Data Access Object)
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
myProject/application/modules/admin/models/Dao/User DB.php
class Admin_Model_Dao_UserDB extends Zend_Db_Tableimplements Admin_Model_Dao_Interface
{//…public function update($id, $username, $pwd, $firstname){
$data = array('username' => $username,'pwd' => $this->hashPassword($pwd),'firstname' => $firstname,
);parent::update($data, 'id = '. (int)$id);
}public function delete($id){
parent::delete('id =' . (int)$id);}public function findAll() {
return $this->fetchAll();}protected function hashPassword($pwd) {
return md5($pwd);}
}?>
Very Simple DAO(Data Access Object)To explain the principleof an extra layer
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
myProject/application/modules/admin/models/User.php
class Admin_Model_User{
const DAO_CLASS = 'Admin_Model_Dao_UserDB';protected $dao;
public function __construct() {$daoClassName = self::DAO_CLASS;$this->dao = new $daoClassName();
}public function find($id){
return $this->dao->find($id);}public function insert($username, $pwd, $firstname){
return $this->dao->insert($username, $pwd, $firstname);}public function update($id, $username, $pwd, $firstname){
return $this->dao->update($id, $username, $pwd, $firstname);}// ...
}
The Model using the DAO.
Easy Switching betweenDifferent data resources
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
myProject/application/modules/admin/models/User.php
<?phpclass Admin_Model_User{
// ...public function delete($id){
return $this->dao->delete($id);}
public function findAll(){
return $this->dao->findAll();}
}
The Model using the DAO.
Easy Switching betweenDifferent data resources
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Actions and Views
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
$ zf create action add index 1 admin$ zf create action edit index 1 admin$ zf create action delete index 1 admin
Setting up the admin actions
Again if you are using a version of Zend_Tool which isn’t fully moduleAware you can patch the Zend_Tool_Project_Provider_ Action withif (self::hasResource($this->_loadedProfile, $name, $controllerName , $module ))
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Admin_IndexController indexAction
public function indexAction(){
$users = new Admin_Model_User();$this->view->users = $users->findAll();
}
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The corresponding index.phtml view script:myProject/application/modules/admin/views/scripts/i ndex/index.phtml
<p><a href="<?php echo $this->url(array('controller'=>'index','action'=>'add'));?>">Add new user</a></p>
<table><tr>
<th>Username</th><th>First Name</th><th> </th>
</tr><?php foreach($this->users as $user) : ?><tr>
<td><?php echo $this->escape($user->username);?></td><td><?php echo $this->escape($user->firstname);?></td><td>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'edit','id'=>$user->id));?>">Edit</a>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'delete','id'=>$user->id));?>">Delete</a>
</td></tr><?php endforeach; ?>
</table>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Admin_IndexController deleteAction
public function deleteAction(){
if ($this->getRequest()->isPost()) {$del = $this->getRequest()->getPost('del');if ($del == 'Yes') {
$id = $this->getRequest()->getPost('id');$users = new Admin_Model_User();$users->delete($id);
}$this->_redirect('/admin/');
} else {$id = $this->_getParam('id', 0);$users = new Admin_Model_User();$this->view->user = $users->find($id);
}}
Zend Framework 1.8 workshop
REST: irreversible action use POST not GET
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The corresponding delete.phtml view script:myProject/application/modules/admin/views/scripts/i ndex/delete.phtml
<p>Are you sure that you want to delete'<?php echo $this->escape($this->user['username']); ?>' aka'<?php echo $this->escape($this->user['firstname']); ?>'?
</p>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post"><fieldset>
<input type="hidden" name="id"value="<?php echo $this->escape($this->user['id']); ?>" />
<input type="submit" name="del" value="Yes" /><input type="submit" name="del" value="No" />
</fieldset></form>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Admin_IndexController addAction
public function addAction(){
if ($this->getRequest()->isPost()) {$action = $this->getRequest()->getPost('action');if ($action == 'add') {
$username = $this->getRequest()->getPost('username');$pwd = $this->getRequest()->getPost('pwd');$firstname = $this->getRequest()->getPost('firstname');$users = new Admin_Model_User();$users->insert($username, $pwd, $firstname);
}$this->_redirect('/admin/');
}}
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The corresponding add.phtml view script:myProject/application/modules/admin/views/scripts/i ndex/add.phtml
<h2>Add a new User</h2><form action="<?php echo $this->url(array('action'=>'add')); ?>" method="post">
<fieldset>username: <input type="text" name="username"><br/>password: <input type="password" name="pwd"><br/>firstname: <input type="text" name="firstname"><br/><input type="submit" name="action" value="add" />
</fieldset></form>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The Admin_IndexController editAction
public function editAction(){
if ($this->getRequest()->isPost()) {$action = $this->getRequest()->getPost('action');if ($action == 'edit') {
$id = $this->getRequest()->getPost('id');$username = $this->getRequest()->getPost('username');$pwd = $this->getRequest()->getPost('pwd');$firstname = $this->getRequest()->getPost('firstname');$users = new Admin_Model_User();$users->update($username, $pwd, $firstname);
}$this->_redirect('/admin/');
} else {$id = $this->_getParam('id', 0);$users = new Admin_Model_User();$this->view->user = $users->find($id);
}}
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Nick BelhommePHP5 Zend Certified Engineer
17 June 2009
The corresponding edit.phtml view script:myProject/application/modules/admin/views/scripts/i ndex/edit.phtml
<h2>Edit User: <?php echo $this->escape($this->user->username); ?></h2><form action="<?php echo $this->url(array('action'=>'edit')); ?>" method="post">
<fieldset><input type="hidden" name="id"
value="<?php echo $this->escape($this->user->id); ?>">username: <input type="text" name="username"
value="<?php echo $this->escape($this->user->username); ?>"><br/>password: <input type="password" name="pwd"><br/>firstname: <input type="text" name="firstname"
value="<?php echo $this->escape($this->user->firstname); ?>"><br/><input type="submit" name="action" value="edit" />
</fieldset></form>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Authentication:
Zend_Auth
Authentication is loosely defined as determining wh ether an entity actually is what it purports to be (i.e., identification), based on some set of credentials.
http://framework.zend.com/manual/en/zend.auth.html# zend.auth.introduction
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
Append the DAO and User Classes
interface Admin_Model_Dao_Interface{
// …public function findCredentials($username, $pwd);
}
class Admin_Model_Dao_UserDB extends Zend_Db_Tableimplements Admin_Model_Dao_Interface
{// …public function findCredentials($username, $pwd){
$select = $this->select()->where('username = ?', $username)->where('pwd = ?', $this->hashPassword($pwd));
$row = $this->fetchRow($select);if($row) {
return $row;}return false;
}}
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
Append the DAO and User Classes
class Admin_Model_User{
// ...public function findCredentials($username, $password){
return $this->dao->findCredentials($username, $password);}
}
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
Always use a vendor or project prefixAnd inform the autoloader of library prefixes
Prepend the application bootstrap
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initAutoloadModuleDefault(){
$autoloader = new Zend_Application_Module_Autoloader(array('namespace' => 'Myp','basePath' => dirname(__FILE__)
));return $autoloader;
}//...
}
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
Create an application model: The Auth AdaptermyProject/application/models/AuthAdapter.php
class Myp_Model_AuthAdapter implements Zend_Auth_Adapter_Interface{
protected $username;protected $password;protected $user;
public function __construct($username, $password) {$this->username = $username;$this->password = $password;$this->user = new Admin_Model_User();
}public function authenticate(){
$match = $this->user->findCredentials($this->username, $this->password);if(!$match) {
$result = new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null);} else {
$user = current($match);$result = new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
}return $result;
}}
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The IndexController loginAction
Zend Framework 1.8 workshop
$ zf create action login index 1
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The IndexController loginActionpublic function loginAction()
{$auth = Zend_Auth::getInstance();if(Zend_Auth::getInstance()->hasIdentity()) {
$this->_redirect('/index/hello');} else if ($this->getRequest()->isPost()) {
$action = $this->getRequest()->getPost('action');if ($action == 'login') {
$username = $this->getRequest()->getPost('username');$pwd = $this->getRequest()->getPost('pwd');$authAdapter = new Myp_Model_AuthAdapter($username, $pwd);$result = $auth->authenticate($authAdapter);if(!$result->isValid()) {
switch ($result->getCode()) {case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
$this->view->error = 'user credentials not found';}
} else {$this->_redirect('/index/hello');
}}
}}
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The corresponding login.phtml view script:myProject/application/views/scripts/index/login.pht ml
<h2>Login</h2><form action="<?php echo $this->url(array('action'=>'login')); ?>" method="post">
<fieldset>username: <input type="text" name="username"><br/>password: <input type="password" name="pwd"><br/><div><?php echo $this->escape($this->error);?></div><input type="submit" name="action" value="login" />
</fieldset></form>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The IndexController helloAction
public function helloAction(){
if(Zend_Auth::getInstance()->hasIdentity()) {$this->view->user = Zend_Auth::getInstance()->getIdentity();
}}
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
The corresponding hello.phtml view script:myProject/application/views/scripts/index/hello.pht ml
<?php if(isset($this->user)):?>welcome <?php echo $this->escape($this->user['firstname']); ?>,to the MyProject application.
<?php else: ?>you need to login first. <a href="<?php echo $this->url(array('action' => 'login')); ?>">login</a>
<?php endif; ?>
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
17 June 2009
Zend Framework 1.8 workshop
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
And that is Authentication.
if(Zend_Auth::getInstance()->hasIdentity())
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
What about?
•Authorization (Zend_Acl)•Filter Input, Escape Output?
•Input Validation (Zend_Validate)•Input filtering (Zend_Filter)
•Dynamic Forms (Zend_Form)
And all the other great stuff???!!!!
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
There is always next time!
1 hour courses, remember??? ;)
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer
Zend Framework 1.8 workshop
17 June 2009
Resources
•http://blog.nickbelhomme.com•http://www.slideshare.net/weierophinney/zend-framew ork-workshop-dpc09•http://en.wikipedia.org/wiki/Zend_Framework•http://blog.astrumfutura.com/archives/373-The-M-in-MV C-Why-Models-are-Misunderstood-and-Unappreciated.html
Source code of this workshop available at:http://blog.nickbelhomme.com/wp-content/uploads/wor kshopzf1.8.zip
Nick BelhommePHP5 Zend Certified Engineer + Zend Framework Certi fied Engineer