際際滷

際際滷Share a Scribd company logo
Zend Framework 
(Bi thuy畉t tr狸nh s畛 d畛ng Zend Framework 1.8) 
Zend framework- AiTi conference kiennt02@aiti.com.vn 1
N畛i dung ch鱈nh 
 Gi畛i thi畛u chung v畛 Zend Framework (ZF) 
 M担 h狸nh MVC trong ZF 
 C畉u tr炭c 畛ng d畛ng vi畉t tr棚n n畛n ZF 
 Gi畛i thi畛u Zend_Controller, Zend_Layout, 
Zend_Config, Zend_Db, Zend_View, 
Zend_Auth 
 Demo 畛ng d畛ng 董n gi畉n vi畉t b畉ng Zend 
Framework 
Zend framework- AiTi conference kiennt02@aiti.com.vn 2
Software framework? 
 Framework: L m畛t c畉u tr炭c m畛i c畛a ng担n ng畛 gi炭p ph叩t 
tri畛n c叩c 畛ng d畛ng ph畉n m畛m. 
 B棚n trong framework c坦 c叩c thnh ph畉n c董 b畉n : 
1. Code library 
2. Scripting language 
3. API 
Ngoi ra c嘆n 動畛c t鱈ch h畛p c叩c ph畉n m畛m ho畉c ng担n ng畛 
kh叩c. 
Zend framework- AiTi conference kiennt02@aiti.com.vn 3
C叩c chu畉n 畛 x但y d畛ng m畛t 
Framework 
 MVC: C坦 h畛 tr畛 Model  Control  View? 
 Multiple DB's: Lm vi畛c 動畛c v畛i nhi畛u lo畉i database? 
 ORM: C坦 h畛 tr畛 object-record mapper? 
 Templates: C坦 h畛 tr畛 cho Template engine? 
 AJAX, Validation, Caching? 
 Auth Module: C坦 module x叩c th畛c ng動畛i d湛ng? 
 Module: T鱈ch h畛p c叩c module ti畛n 鱈ch nh動 PDF, RSS, 
揃 EDPnew : (Event Driven Programming). H動畛ng s畛 ki畛n? 
Zend framework- AiTi conference kiennt02@aiti.com.vn 4
C叩c framework ph畛 bi畉n 
 Zend 
 Cake 
 CodeIgniter 
 Doctrine 
 Propel 
 Spring 
 .NET framework 
Zend framework- AiTi conference kiennt02@aiti.com.vn 5
M担 h狸nh MVC 
Zend framework- AiTi conference kiennt02@aiti.com.vn 6
MVC[1] 
 Control: L畛p X畛 l箪 c叩c Bussines case 
 Model: L畛p giao ti畉p v畛i Database 
 View: L畛p x畛 l箪 cho Bussines logic 
 C董 ch畉 ho畉t 畛ng 董n gi畉n: 
Khi m畛t request 動畛c g畛i t畛i, l畛p C x叩c 畛nh y棚u c畉u 
畛 l畉y data t畛 l畛p M sau 坦 tr畉 v畛 cho l畛p V hi畛n th畛 
Zend framework- AiTi conference kiennt02@aiti.com.vn 7
Zend Framework 
 Zend Framework l s畉n ph畉m framework m達 
ngu畛n m畛 動畛c ph叩t tri畛n tr棚n n畛n PHP 5.0 
theo chu畉n h動畛ng 畛i t動畛ng. 
 Zend Framwork l framework theo m担 h狸nh 
MVC. 
 Zend Framework c坦 h畛 tr畛 lm vi畛c v畛i 
Tempalate engine k畉t h畛p c湛ng t畉ng View 
Zend framework- AiTi conference kiennt02@aiti.com.vn 8
M担 h狸nh MVC c董 b畉n trong 
畛ng d畛ng c畛a Zend 
Zend framework- AiTi conference kiennt02@aiti.com.vn 9
Zend framework lm vi畛c th畉 no? 
C叩c l畛p 動畛c ph但n c畉p theo t棚n th動 m畛c 
V畛i class Zend_Db_Table 
T動董ng 畛ng ch炭ng ta c坦 c但y th動 m畛c 
Zend 
|_Db 
|_Table 
|_ class Zend_Db_Table.php 
Zend framework- AiTi conference kiennt02@aiti.com.vn 10
Zend framework lm vi畛c th畉 no?[1] 
 Controller th畛 hi畛n tr棚n URL 
http://domainname/controller/action/getID/value 
http://domainname/module/controller/action/getID/value 
V鱈 d畛 
http://localhost/zfdemo/admin/: T狸m t畛i indexAction trong adminController 畛 th畛c thi 
http://localhost/zfdemo/admin/login: T狸m t畛i loginAction trong controller adminController 
畛 th畛c thi 
http://zend.com/news/views/id/15: T狸m t畛i vewsAction trong controller newsController v 
get id=15 畛 th畛c thi. 
Zend framework- AiTi conference kiennt02@aiti.com.vn 11
M担 h狸nh MVC trong Zend Framework 
 Model : Cung c畉p t畉p h畛p c叩c l畛p 動畛c tr畛u t動畛ng 
h坦a s畛 d畛ng cho vi畛c truy xu畉t d畛 li畛u. 
L畛p ph畛c v畛: Zend_DB, Zend_DB_Table 
 View : 畛nh ngh挑a c叩c th担ng tin hi畛n th畛 ph鱈a ng動畛i 
d畛ng sau khi 動畛c x畛 l箪 v tr畉 v畛 t畛 controller. 
L畛p ph畛c v畛: Zend_View 
 Controller : Ki畛m so叩t d畛 li畛u vo ra. Xu畉t th担ng tin 
ra t畉ng View khi 動畛c th畛c thi. 
L畛p ph畛c v畛: Zend_Controller 
Zend framework- AiTi conference kiennt02@aiti.com.vn 12
M担 h狸nh MVC c畛a Zend 
Zend framework- AiTi conference kiennt02@aiti.com.vn 13
C畉u tr炭c th動 m畛c c畛a project 
Zend framework- AiTi conference kiennt02@aiti.com.vn 14
Kh畛i t畉o 畛ng d畛ng trong Zend 
Framework 
 畛 kh畛i t畉o 畛ng d畛ng ZF ch炭ng ta tr畉i qua 3 
b動畛c c董 b畉n: 
1. Kh畛i t畉o m担i tr動畛ng: B畉t ch畛c nng Debug, 
khai b叩o v畛 m炭i gi畛 lm vi畛c,  
2. Khai b叩o 動畛ng d畉n: Load c叩c class lm 
vi畛c c畛a nh但n ZF 
3. Thi畉t l畉p controllers: Ch畛 ra n董i 叩p 畛ng c叩c 
request. 
Zend framework- AiTi conference kiennt02@aiti.com.vn 15
Gi畛i thi畛u c叩c l畛p c董 b畉n theo 
m担 h狸nh MVC 
 Zend_Config 
 Zend_Db 
 Zend_Controller 
 Zend_View 
 Zend_Auth 
Zend framework- AiTi conference kiennt02@aiti.com.vn 16
Zend_config 
 Ch畛c nng: Load c畉u h狸nh giao ti畉p webserver: Databse, c叩c 
動畛ng d畉n l動u tr畛 Layout, css, js 
 C坦 2 l畛p 畉i di畛n 
Zend_Config_Ini: Cho ph辿p nh畉n di畛n v 畛c c叩c file . ini, 
.txt,  
Zend_Config_Xml: Cho ph辿p nh畉n di畛n v 畛c file .xml 
Zend framework- AiTi conference kiennt02@aiti.com.vn 17
V鱈 d畛 v畛 Zend_Config 
 File config.ini 
[database] 
db.adapter = PDO_MYSQL 
db.config.host = localhost 
db.config.username = demo_user 
db.config.password = 1234 
db.config.dbname = newsDb 
 Load c畉u h狸nh trong file .ini 
$conDatabase = new 
Zend_Config_Ini('../config/config.ini','database'); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 18
Khai b叩o c畉u h狸nh & kh畛i t畉o 
Thi畉t l畉p m担i tr動畛ng 
error_reporting(E_ALL|E_STRICT); 
date_default_timezone_set('Europe/London'); 
 畛nh ngh挑a 動畛ng d畉n 畉n th動 vi畛n c畛a Zend 
Framework 
set_include_path('../library/'. PATH_SEPARATOR . '../application/models'); 
 G畛i l畛p Zend_Loader 
include "Zend/Loader.php"; 
 G畛i c叩c l畛p 動畛c s畛 d畛ng 
Zend_Loader::loadClass('Zend_Controller_Front'); 
Zend_Loader::loadClass('Zend_Registry'); 
Zend_Loader::loadClass('Zend_Layout'); 
Zend_Loader::loadClass('Zend_View'); 
Zend_Loader::loadClass('Zend_Config_Ini'); 
Zend_Loader::loadClass('Zend_Db'); 
Zend_Loader::loadClass('Zend_Db_Table'); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 19
Khai b叩o c畉u h狸nh & kh畛i t畉o [1] 
 Load th担ng c畉u h狸nh database, kh畛i t畉o bi畉n ton c畛c 
$conDatabase = new Zend_Config_Ini('../config/config.ini','database'); 
$registry = Zend_Registry::getInstance(); 
$registry->set('conDatabase', $conDatabase); 
 K畉t n畛i CSDL 
$db = Zend_Db::factory($conDatabase->db->adapter, $conDatabase->db->config->toArray()); 
Zend_Db_Table::setDefaultAdapter($db); 
$registry = Zend_Registry::getInstance(); 
$registry->set('db', $db); 
 Load c畉u h狸nh 動畛ng d畉n c畛a 畛ng d畛ng 
$conDirApp = new Zend_Config_Ini('../config/config.ini','dir'); 
$registry = Zend_Registry::getInstance(); 
$registry->set('conDirApp', $conDirApp); 
 Thi畉t l畉p Controller 
$frontController = Zend_Controller_Front::getInstance(); 
$frontController->addControllerDirectory('../application/controllers'); 
$frontController->throwExceptions(true); 
$frontController->dispatch(); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 20
Zend_Db 
 Ch畛c nng: Giao ti畉p v畛i Database 
Load c畉u h狸nh connection 
 Zend_Db_Table: C叩c thao t叩c th畛c thi v畛i Table, Row, Column 
 Zend_Db_Select: Thay th畉 v t畉o ra c叩ch vi畉t c但u l畛nh SQL query m畛i 
 L畛p ny 動畛c s畛 d畛ng trong t畉ng Model v giao ti畉p tr畛c ti畉p 
v畛i Database 
Zend framework- AiTi conference kiennt02@aiti.com.vn 21
C畉u h狸nh s畛 d畛ng l畛p Zend_DB 
 K畉t n畛i database s畛 d畛ng 
Adapter 
$db = Zend_Db::factory('PDO_MYSQL', $params); 
Zend_Db_Table::setDefaultAdapter($db); 
 Khai b叩o bi畉n ton c畛c 
$registry = Zend_Registry::getInstance(); 
$registry->set('db', $db); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 22
V鱈 d畛 v畛 Model tr棚n Zend_Db 
 C坦 th畛 d湛ng truy v畉n 畛 th畛c hi畛n thao t叩c 
v畛i CSDL 
 $sql = SELECT * FROM demo_table WHERE demo_id = 1; 
 $result = $db->query($sql); 
 Ho畉c s畛 d畛ng Zend_DB_Select 
 $select = new Zend_Db_Select($db); 
 $select->from(demo_table'); 
 $select->where(demo_id = ?', 1'); 
 $result = $select->query(); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 23
V鱈 d畛 v畛 Model tr棚n Zend_Db[1] 
 Khai b叩o l畛p trong Model 
class Booking extends Zend_Db_Table 
{ 
protected $_name = 'booking_region_tmp '; 
protected $_primary = 'booking_id '; 
public function loadListRegion($booking_id, $getAll=false) { 
$query = $this->_db->select() 
->from('booking_region_tmp') 
->where('booking_id = ?', $booking_id) 
->order('booking_region_id'); 
$result = $this->_db->fetchAll($query); 
return $result; 
} 
} 
Zend framework- AiTi conference kiennt02@aiti.com.vn 24
Gi畛i thi畛u v畛 Zend Controller 
 Controller trong ZF 動畛c 畉t t棚n theo quy 
畛nh 畛 s畛 d畛ng cho vi畛c g畛i hm trong 
Controller 坦 qua tham s畛 tr棚n URL. 
 Vi畛c x畛 l箪 Request 動畛c th畛c hi畛n th担ng qua 
ph動董ng th畛c 動畛c khai b叩o trong Controller 
t動董ng 畛ng. 
Zend framework- AiTi conference kiennt02@aiti.com.vn 25
V鱈 d畛 v畛 Zend Controller 
 AdminController.php t畉p h畛p c叩c action x畛 l箪 trong ph畉n admin . 
Trong l畛p ny c坦 ph動董ng th畛c addAction() tr畉 v畛 k畉 qu畉 Add 
me! : 
 <?php 
 Zend::LoadClass('Zend_View'); 
 class AdminController extends Zend_Controller_Action 
 { 
 public function addAction() { 
 $ this->view->assign('title', Add me!'); } 
 } 
 Vi畛c g畛i action ny 動畛c th畛c hi畛n qua URL request nh動 sau :: 
http://localhost/public/admin/add 
 (trong 坦 http://localhost/public l 動畛ng d畉n c畛a 畛ng d畛ng, 
admin : t棚n controller, add : action th畛c hi畛n trong controller 
admin) 
Zend framework- AiTi conference kiennt02@aiti.com.vn 26
Zend_View 
 Th畛c thi Zend_View_Interface gi炭p t畉o ra 
template engine ri棚ng. 
 M畉c 畛nh Zend_View s畛 d畛ng PHP Taglib 
 G叩n v tr畉 v畛 gi叩 tr畛 d動畛i d畉ng 畛i t動畛ng . 
$view->content = $body 
 Truy c畉p bi畉n trong view s畛 d畛ng con tr畛 $this 
 <?= $this->content ?> 
Zend framework- AiTi conference kiennt02@aiti.com.vn 27
V鱈 d畛 Zend_View 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> <?php echo $this->escape($this->pageTitle); ?> </title> 
</head> 
<body> 
<div id="page"> 
<div id="title"><h2>Title</h2></div> 
<div id="content"> 
<?php echo $this->layout()->content ?> 
</div> 
</div> 
</body> 
</html> 
Zend framework- AiTi conference kiennt02@aiti.com.vn 28
X叩c th畛c ng動畛i d湛ng b畉ng 
Zend_Auth 
 //Bien duoc truyen vao tu form 
$username= $this->getRequest()->getPost('username'); 
$password= $this->getRequest()->getPost('password'); 
Zend_Loader::loadClass('Zend_Auth'); 
 //Kiem tra tai khoan trong CSDL 
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 
'sysadm', 
'username', 
'password', 
'MD5(?) AND status != "compromised"'); 
$authAdapter->setIdentity($username) 
->setCredential($password); 
 //Tra ve ket qua xac thuc 
$result= $authAdapter->authenticate(); 
Zend framework- AiTi conference kiennt02@aiti.com.vn 29
Demo 畛ng d畛ng s畛 d畛ng ZF t畛 ch畛c 
theo m担 h狸nh MVC 
Bi thuy畉t tr狸nh 畉n 但y k畉t th炭c 
Ch坦c c存c b孫n c達 m辿t nam m鱈i v鱈i nhiu 
th亮nh c束ng m鱈i, tr亮n 速y nim vui v亮 
h孫nh ph坦c 
AITI-APTECH 
Zend framework- AiTi conference kiennt02@aiti.com.vn 30

More Related Content

Co ban ve_zend_framework 1

  • 1. Zend Framework (Bi thuy畉t tr狸nh s畛 d畛ng Zend Framework 1.8) Zend framework- AiTi conference kiennt02@aiti.com.vn 1
  • 2. N畛i dung ch鱈nh Gi畛i thi畛u chung v畛 Zend Framework (ZF) M担 h狸nh MVC trong ZF C畉u tr炭c 畛ng d畛ng vi畉t tr棚n n畛n ZF Gi畛i thi畛u Zend_Controller, Zend_Layout, Zend_Config, Zend_Db, Zend_View, Zend_Auth Demo 畛ng d畛ng 董n gi畉n vi畉t b畉ng Zend Framework Zend framework- AiTi conference kiennt02@aiti.com.vn 2
  • 3. Software framework? Framework: L m畛t c畉u tr炭c m畛i c畛a ng担n ng畛 gi炭p ph叩t tri畛n c叩c 畛ng d畛ng ph畉n m畛m. B棚n trong framework c坦 c叩c thnh ph畉n c董 b畉n : 1. Code library 2. Scripting language 3. API Ngoi ra c嘆n 動畛c t鱈ch h畛p c叩c ph畉n m畛m ho畉c ng担n ng畛 kh叩c. Zend framework- AiTi conference kiennt02@aiti.com.vn 3
  • 4. C叩c chu畉n 畛 x但y d畛ng m畛t Framework MVC: C坦 h畛 tr畛 Model Control View? Multiple DB's: Lm vi畛c 動畛c v畛i nhi畛u lo畉i database? ORM: C坦 h畛 tr畛 object-record mapper? Templates: C坦 h畛 tr畛 cho Template engine? AJAX, Validation, Caching? Auth Module: C坦 module x叩c th畛c ng動畛i d湛ng? Module: T鱈ch h畛p c叩c module ti畛n 鱈ch nh動 PDF, RSS, 揃 EDPnew : (Event Driven Programming). H動畛ng s畛 ki畛n? Zend framework- AiTi conference kiennt02@aiti.com.vn 4
  • 5. C叩c framework ph畛 bi畉n Zend Cake CodeIgniter Doctrine Propel Spring .NET framework Zend framework- AiTi conference kiennt02@aiti.com.vn 5
  • 6. M担 h狸nh MVC Zend framework- AiTi conference kiennt02@aiti.com.vn 6
  • 7. MVC[1] Control: L畛p X畛 l箪 c叩c Bussines case Model: L畛p giao ti畉p v畛i Database View: L畛p x畛 l箪 cho Bussines logic C董 ch畉 ho畉t 畛ng 董n gi畉n: Khi m畛t request 動畛c g畛i t畛i, l畛p C x叩c 畛nh y棚u c畉u 畛 l畉y data t畛 l畛p M sau 坦 tr畉 v畛 cho l畛p V hi畛n th畛 Zend framework- AiTi conference kiennt02@aiti.com.vn 7
  • 8. Zend Framework Zend Framework l s畉n ph畉m framework m達 ngu畛n m畛 動畛c ph叩t tri畛n tr棚n n畛n PHP 5.0 theo chu畉n h動畛ng 畛i t動畛ng. Zend Framwork l framework theo m担 h狸nh MVC. Zend Framework c坦 h畛 tr畛 lm vi畛c v畛i Tempalate engine k畉t h畛p c湛ng t畉ng View Zend framework- AiTi conference kiennt02@aiti.com.vn 8
  • 9. M担 h狸nh MVC c董 b畉n trong 畛ng d畛ng c畛a Zend Zend framework- AiTi conference kiennt02@aiti.com.vn 9
  • 10. Zend framework lm vi畛c th畉 no? C叩c l畛p 動畛c ph但n c畉p theo t棚n th動 m畛c V畛i class Zend_Db_Table T動董ng 畛ng ch炭ng ta c坦 c但y th動 m畛c Zend |_Db |_Table |_ class Zend_Db_Table.php Zend framework- AiTi conference kiennt02@aiti.com.vn 10
  • 11. Zend framework lm vi畛c th畉 no?[1] Controller th畛 hi畛n tr棚n URL http://domainname/controller/action/getID/value http://domainname/module/controller/action/getID/value V鱈 d畛 http://localhost/zfdemo/admin/: T狸m t畛i indexAction trong adminController 畛 th畛c thi http://localhost/zfdemo/admin/login: T狸m t畛i loginAction trong controller adminController 畛 th畛c thi http://zend.com/news/views/id/15: T狸m t畛i vewsAction trong controller newsController v get id=15 畛 th畛c thi. Zend framework- AiTi conference kiennt02@aiti.com.vn 11
  • 12. M担 h狸nh MVC trong Zend Framework Model : Cung c畉p t畉p h畛p c叩c l畛p 動畛c tr畛u t動畛ng h坦a s畛 d畛ng cho vi畛c truy xu畉t d畛 li畛u. L畛p ph畛c v畛: Zend_DB, Zend_DB_Table View : 畛nh ngh挑a c叩c th担ng tin hi畛n th畛 ph鱈a ng動畛i d畛ng sau khi 動畛c x畛 l箪 v tr畉 v畛 t畛 controller. L畛p ph畛c v畛: Zend_View Controller : Ki畛m so叩t d畛 li畛u vo ra. Xu畉t th担ng tin ra t畉ng View khi 動畛c th畛c thi. L畛p ph畛c v畛: Zend_Controller Zend framework- AiTi conference kiennt02@aiti.com.vn 12
  • 13. M担 h狸nh MVC c畛a Zend Zend framework- AiTi conference kiennt02@aiti.com.vn 13
  • 14. C畉u tr炭c th動 m畛c c畛a project Zend framework- AiTi conference kiennt02@aiti.com.vn 14
  • 15. Kh畛i t畉o 畛ng d畛ng trong Zend Framework 畛 kh畛i t畉o 畛ng d畛ng ZF ch炭ng ta tr畉i qua 3 b動畛c c董 b畉n: 1. Kh畛i t畉o m担i tr動畛ng: B畉t ch畛c nng Debug, khai b叩o v畛 m炭i gi畛 lm vi畛c, 2. Khai b叩o 動畛ng d畉n: Load c叩c class lm vi畛c c畛a nh但n ZF 3. Thi畉t l畉p controllers: Ch畛 ra n董i 叩p 畛ng c叩c request. Zend framework- AiTi conference kiennt02@aiti.com.vn 15
  • 16. Gi畛i thi畛u c叩c l畛p c董 b畉n theo m担 h狸nh MVC Zend_Config Zend_Db Zend_Controller Zend_View Zend_Auth Zend framework- AiTi conference kiennt02@aiti.com.vn 16
  • 17. Zend_config Ch畛c nng: Load c畉u h狸nh giao ti畉p webserver: Databse, c叩c 動畛ng d畉n l動u tr畛 Layout, css, js C坦 2 l畛p 畉i di畛n Zend_Config_Ini: Cho ph辿p nh畉n di畛n v 畛c c叩c file . ini, .txt, Zend_Config_Xml: Cho ph辿p nh畉n di畛n v 畛c file .xml Zend framework- AiTi conference kiennt02@aiti.com.vn 17
  • 18. V鱈 d畛 v畛 Zend_Config File config.ini [database] db.adapter = PDO_MYSQL db.config.host = localhost db.config.username = demo_user db.config.password = 1234 db.config.dbname = newsDb Load c畉u h狸nh trong file .ini $conDatabase = new Zend_Config_Ini('../config/config.ini','database'); Zend framework- AiTi conference kiennt02@aiti.com.vn 18
  • 19. Khai b叩o c畉u h狸nh & kh畛i t畉o Thi畉t l畉p m担i tr動畛ng error_reporting(E_ALL|E_STRICT); date_default_timezone_set('Europe/London'); 畛nh ngh挑a 動畛ng d畉n 畉n th動 vi畛n c畛a Zend Framework set_include_path('../library/'. PATH_SEPARATOR . '../application/models'); G畛i l畛p Zend_Loader include "Zend/Loader.php"; G畛i c叩c l畛p 動畛c s畛 d畛ng Zend_Loader::loadClass('Zend_Controller_Front'); Zend_Loader::loadClass('Zend_Registry'); Zend_Loader::loadClass('Zend_Layout'); Zend_Loader::loadClass('Zend_View'); Zend_Loader::loadClass('Zend_Config_Ini'); Zend_Loader::loadClass('Zend_Db'); Zend_Loader::loadClass('Zend_Db_Table'); Zend framework- AiTi conference kiennt02@aiti.com.vn 19
  • 20. Khai b叩o c畉u h狸nh & kh畛i t畉o [1] Load th担ng c畉u h狸nh database, kh畛i t畉o bi畉n ton c畛c $conDatabase = new Zend_Config_Ini('../config/config.ini','database'); $registry = Zend_Registry::getInstance(); $registry->set('conDatabase', $conDatabase); K畉t n畛i CSDL $db = Zend_Db::factory($conDatabase->db->adapter, $conDatabase->db->config->toArray()); Zend_Db_Table::setDefaultAdapter($db); $registry = Zend_Registry::getInstance(); $registry->set('db', $db); Load c畉u h狸nh 動畛ng d畉n c畛a 畛ng d畛ng $conDirApp = new Zend_Config_Ini('../config/config.ini','dir'); $registry = Zend_Registry::getInstance(); $registry->set('conDirApp', $conDirApp); Thi畉t l畉p Controller $frontController = Zend_Controller_Front::getInstance(); $frontController->addControllerDirectory('../application/controllers'); $frontController->throwExceptions(true); $frontController->dispatch(); Zend framework- AiTi conference kiennt02@aiti.com.vn 20
  • 21. Zend_Db Ch畛c nng: Giao ti畉p v畛i Database Load c畉u h狸nh connection Zend_Db_Table: C叩c thao t叩c th畛c thi v畛i Table, Row, Column Zend_Db_Select: Thay th畉 v t畉o ra c叩ch vi畉t c但u l畛nh SQL query m畛i L畛p ny 動畛c s畛 d畛ng trong t畉ng Model v giao ti畉p tr畛c ti畉p v畛i Database Zend framework- AiTi conference kiennt02@aiti.com.vn 21
  • 22. C畉u h狸nh s畛 d畛ng l畛p Zend_DB K畉t n畛i database s畛 d畛ng Adapter $db = Zend_Db::factory('PDO_MYSQL', $params); Zend_Db_Table::setDefaultAdapter($db); Khai b叩o bi畉n ton c畛c $registry = Zend_Registry::getInstance(); $registry->set('db', $db); Zend framework- AiTi conference kiennt02@aiti.com.vn 22
  • 23. V鱈 d畛 v畛 Model tr棚n Zend_Db C坦 th畛 d湛ng truy v畉n 畛 th畛c hi畛n thao t叩c v畛i CSDL $sql = SELECT * FROM demo_table WHERE demo_id = 1; $result = $db->query($sql); Ho畉c s畛 d畛ng Zend_DB_Select $select = new Zend_Db_Select($db); $select->from(demo_table'); $select->where(demo_id = ?', 1'); $result = $select->query(); Zend framework- AiTi conference kiennt02@aiti.com.vn 23
  • 24. V鱈 d畛 v畛 Model tr棚n Zend_Db[1] Khai b叩o l畛p trong Model class Booking extends Zend_Db_Table { protected $_name = 'booking_region_tmp '; protected $_primary = 'booking_id '; public function loadListRegion($booking_id, $getAll=false) { $query = $this->_db->select() ->from('booking_region_tmp') ->where('booking_id = ?', $booking_id) ->order('booking_region_id'); $result = $this->_db->fetchAll($query); return $result; } } Zend framework- AiTi conference kiennt02@aiti.com.vn 24
  • 25. Gi畛i thi畛u v畛 Zend Controller Controller trong ZF 動畛c 畉t t棚n theo quy 畛nh 畛 s畛 d畛ng cho vi畛c g畛i hm trong Controller 坦 qua tham s畛 tr棚n URL. Vi畛c x畛 l箪 Request 動畛c th畛c hi畛n th担ng qua ph動董ng th畛c 動畛c khai b叩o trong Controller t動董ng 畛ng. Zend framework- AiTi conference kiennt02@aiti.com.vn 25
  • 26. V鱈 d畛 v畛 Zend Controller AdminController.php t畉p h畛p c叩c action x畛 l箪 trong ph畉n admin . Trong l畛p ny c坦 ph動董ng th畛c addAction() tr畉 v畛 k畉 qu畉 Add me! : <?php Zend::LoadClass('Zend_View'); class AdminController extends Zend_Controller_Action { public function addAction() { $ this->view->assign('title', Add me!'); } } Vi畛c g畛i action ny 動畛c th畛c hi畛n qua URL request nh動 sau :: http://localhost/public/admin/add (trong 坦 http://localhost/public l 動畛ng d畉n c畛a 畛ng d畛ng, admin : t棚n controller, add : action th畛c hi畛n trong controller admin) Zend framework- AiTi conference kiennt02@aiti.com.vn 26
  • 27. Zend_View Th畛c thi Zend_View_Interface gi炭p t畉o ra template engine ri棚ng. M畉c 畛nh Zend_View s畛 d畛ng PHP Taglib G叩n v tr畉 v畛 gi叩 tr畛 d動畛i d畉ng 畛i t動畛ng . $view->content = $body Truy c畉p bi畉n trong view s畛 d畛ng con tr畛 $this <?= $this->content ?> Zend framework- AiTi conference kiennt02@aiti.com.vn 27
  • 28. V鱈 d畛 Zend_View <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> <?php echo $this->escape($this->pageTitle); ?> </title> </head> <body> <div id="page"> <div id="title"><h2>Title</h2></div> <div id="content"> <?php echo $this->layout()->content ?> </div> </div> </body> </html> Zend framework- AiTi conference kiennt02@aiti.com.vn 28
  • 29. X叩c th畛c ng動畛i d湛ng b畉ng Zend_Auth //Bien duoc truyen vao tu form $username= $this->getRequest()->getPost('username'); $password= $this->getRequest()->getPost('password'); Zend_Loader::loadClass('Zend_Auth'); //Kiem tra tai khoan trong CSDL $authAdapter = new Zend_Auth_Adapter_DbTable($db, 'sysadm', 'username', 'password', 'MD5(?) AND status != "compromised"'); $authAdapter->setIdentity($username) ->setCredential($password); //Tra ve ket qua xac thuc $result= $authAdapter->authenticate(); Zend framework- AiTi conference kiennt02@aiti.com.vn 29
  • 30. Demo 畛ng d畛ng s畛 d畛ng ZF t畛 ch畛c theo m担 h狸nh MVC Bi thuy畉t tr狸nh 畉n 但y k畉t th炭c Ch坦c c存c b孫n c達 m辿t nam m鱈i v鱈i nhiu th亮nh c束ng m鱈i, tr亮n 速y nim vui v亮 h孫nh ph坦c AITI-APTECH Zend framework- AiTi conference kiennt02@aiti.com.vn 30