You are on page 1of 9

Tm hiu v Zend

I.

Zend_Controller 1. Khi nim

Zend_Controller l lp c chc nng nhn cc yu cu t client, sau gi ti cc phng thc ca lp tng ng trong model, sau chuyn d liu ra cho view hin th ra mn hnh. 2. S dng

To cc file controller trong th mc Controller ca ng dng, vi 1 NameController tng ng vi 1 th mc trong views/script c tn Name. Trong th mc ny li bao gm cc file .phtml tng ng vi mi action c trong file Controller. Quy c cch t tn file Controller l tenController .php. Mi controller ny s l 1 class, class ny phi k tha t class Zend_Controller_Action V d:
<?php class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->assign('title', 'Hello, World!'); } } ?>

II.

Zend_View 1. Khi nim

Zend View c nhim v hin th thng tin ra mn hnh cho ngi dng. Trong controller ca zend framework khi chng ta khi to mt action (hnh ng). Th tng ng ti th mc views/scripts/tn_controller/hnh_ng.phtml. V d: Nu l NewsController v addAction() th chng ta s to view theo cu trc: Views/scripts/news/add.phtml Zend Framework s t ng gi views ca action add khi ngi dng tham chiu ti action add qua ng dn. Vy nhng g m chng ta trnh by view add.phtml s hin th cho ngi dng thy khi h tham chiu ti action add. Ti cc action ca controller mun truyn bin ra view ta vit nh sau: TH1: Truyn bin l 1 xu: $this->view->ten_bien = gi tr
V d:

Ta to controller tn IndexController c ni dung nh sau:


<?php class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->s1= "DEMO ZEND VIEWSVAdesign.vn"; } }

Ti th mc views/scripts/index ta to file tn index.phtml vi ni dung:


<?php echo "<h1>".$this->s1."</h1>"; ?>

V kt qu s ra l: DEMO ZEND VIEWS VAdesign.vn TH2: trng hp mun truyn mt mng sang view trong controller ta lm nh sau:
<?php class IndexController extends Zend_Controller_Action{ public function indexAction() { $data= array( 'title' => 'Zend Framework', 'author'=> 'Kenny',
2

'date' => '20-11-2009', 'website'=> 'www.QHOnline.Info' ); $this->view->info =$data; } }

view ta vit nh sau:


<?php foreach($this->info as $k => $v) { echo "<b>$k</b>: $v<br />"; } ?>

Ngoi ra Zend_view h tr ngi dng nhng th vin cu hnh c bn trong HTML nh <title>, <link>, <script III. Zend_DB 1. Khi nim Zend_Db l mt lp quan trng trong Zend Framework. N gip chng ta thao tc vi database mt cch d dng v linh hot. Gip ta c th kt ni vi nhiu h CSDL khc nhau. 2. S dng i) Khai bo kt ni c s d liu Ta khai bo kt ni c s d liu ti file application/configs/application.ini nh sau:
resources.db.adapter = "Pdo_mysql" resources.db.params.host = "localhost" resources.db.params.username = "root" resources.db.params.password = "" resources.db.params.dbname = "qhonline"

Dng th 1 l khai bo loi c s d liu. dng th 2 l khai bo my ch cha csdl. Dng th 3 khai bo tn ng nhp vo csdl. Dng th 4 khai bo mt khu. dng th 5 khai bo tn ca csdl. M file bootstrap.php thm on code sau:
<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{ protected function _initDatabase(){ $db = $this->getPluginResource('db')->getDbAdapter();
3

Zend_Registry::set('db', $db); } }

on code trn, ta thm vo phng thc initDatabase(). y l phng thc s c triu gi khi chy ng dng. Ti y, ta ly thng tin kt ni t ni dung cu hnh file application.ini. Gi s ta c 1 CSDL qun l ngi dng. thao tc vi d liu, ta to file User.php trong th mc Models nh sau:

<?php class Model_User{ protected $db; public function __construct(){ $this->db=Zend_Registry::get('db'); } public function listall(){ $sql=$this->db->query("select * from user order by id DESC"); return $sql->fetchAll(); } }

Phng thc listall trong class Model_User c chc nng lit k ton b ngi dng c trong bng users. Tip tc, ti controller User ta gi model nh sau:
<?php class UserController extends Zend_Controller_Action{ public function indexAction(){ $muser=new Model_User; $data=$muser->listall(); echo "<pre>"; print_r($data); echo "</pre>"; } }

on code trn gi model user v gi tip phng thc listall() ca model hin th thng tin ca ngi dng bi cc th <pre></pre> bn di. IV. Zend_Auth 1. Khi nim Zend Auth l mt lp cung cp cho chng ta nhiu phng thc x l gip chng ta n gin ha thc hin qu trnh chng thc ti khon c trong h thng ng
4

dng. Zend Auth h tr cho chng ta trong qu trnh ng nhp, thot v ly cc thng tin cn thit ca ti khon ngi s dng ang ng nhp 2. S dng Zend_Auth khng ring l m kt hp cc lp khc nhau nh Zend_Db_Table v Zend_Session. Mt s phng thc c bn: - getInstance() : Khi to Zend Autho V d:
$auth = Zend_Auth::getInstance ();

- Zend_Auth_Adapter_DbTable():Kt ni table cha d liu V d:


$authAdapter = new Zend_Auth_Adapter_DbTable($db); $authAdapter->setTableName(user) ->setIdentityColumn(UserName) ->setCredentialColumn(Password);

- setIdentity(): so snh vi d liu t Form


$authAdapter->setIdentity($uname); $authAdapter->setCredential($paswd);

- authenticate($authAdapter) : Ly kt qu truy vn trong database V d:


$result = $auth->authenticate($authAdapter);

- isValid(): kim tra kt qu truy vn V d:


$result->isValid()

- getResultRowObject(): ly d liu cn thit trong bng csdl

$data = $authAdapter>getResultRowObject($returnColum,$omitColum)

Trong : $returnColum: mng cha tn cc ct cn ly $omitColum: mng cha tn cc ct ko ly - getStorage(): ly d liu lu vo session v d:
$auth->getStorage()->write($data);

- hasIndentity(): kim tra ti khon chng thc hay cha V d: $auth->hasIdentity() - getIndetity(): ly tn ti khon
$User=$auth->getIdentity()

- clearIndentity(): xo chng thc ti khon


$auth->clearIdentity();

V.

Zend Filter 1. Khi nim

L mt th vin cung cp cho chng ta nhng lp chuyn i cc kiu d liu v ng nh dng chng ta mun, hoc s dng ly ra nhng d liu cn thit trong mt chui k t no . 2. S dng Mt s phng thc thng dng trong Zend_Filter: - Zend_Filter_Alnum : loi b nhng k t khng phi l alphabetic v number V d:
$str = 'asdasd \#$%@&*!)(&^^%#123jasd'; $filter = new Zend_Filter_Alnum(); $result = $filter->filter($str);

echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

- Zend_Filter_Alpha V d:
$str = 'asdasd \#$%@&*!)(&^^%#123jasd'; $filter = new Zend_Filter_Alpha(); $result = $filter->filter($str); echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

- Zend_Filter_BaseName: ly tn tp tin t 1 ng dn: V d:


$str = 'http://framework.zend.com/manual/en/zend.filter.set.html'; $filter = new Zend_Filter_BaseName(); $result = $filter->filter($str); echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

Zend_Filter_Dir: ly ng dn n th mc V d:
$str = 'http://framework.zend.com/manual/en/zend.filter.set.html'; $filter = new Zend_Filter_Dir(); $result = $filter->filter($str); echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

- Zend_Filter_Callback

V d:
$str = 'I love Zend Framework'; $filter = new Zend_Filter_Callback('strrev'); $result = $filter->filter($str); echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

- Zend_Filter_Digits: ly ra s nguyn trong mt chui V d:


$str = '0123.12 sd'; $filter = new Zend_Filter_Digits(); $result = $filter->filter($str); echo '<br>Source: ' . $str; echo '<br>Filter: ' . $result;

VI. Zend Layout 1. Khi nim Khi s dng layout, th mi d liu ca view u s hin th trong vng ch nh ca layout m chng ta khng cn phi khai bo chng trong tng view mt. Nh vy, ni cch khc chng ta s khng cn quan tm n b cc ca layout khi lm vic vi view. M ch quan tm n d liu, vai tr m view th hin nh th no m thi. 2. S dng Khai bo s dng layout Trong file application.ini ta thm vo 2 dng sau:
resources.layout.layout="layout" resources.layout.layoutPath=APPLICATION_PATH "/layouts/scripts"

Dng u tin khai bo tn ca file cha layout


8

Dng th hai khai bo ng dn ti file layout. K ti, ta to th mc layouts/scripts. V to file layout.phtml c ni dung:
<?php echo $this->doctype() ?> <html> <head> <?php echo $this->headTitle() ?> <?php echo $this->headMeta() ?> <?php echo $this->headLink() ?> <?php echo $this->headScript() ?> </head> <body> <?php echo $this->layout()->content; ?> </body> </html>

Cu lnh echo $this->layout()->content vo hin th trn b cc ny.

gi cc view ca controller

You might also like