30
Zend framework- AiTi conference [email protected] 1 Zend Framework (Bài thuyết trình sử dụng Zend Framework 1.8)

Zend Framework Tutorial - dulieu.tailieuhoctap.vndulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/file... · Mô hình MVC trong Zend Framework Model : Cung cấp tập

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Zend framework- AiTi conference [email protected] 1

Zend Framework

(Bài thuyết trình sử dụng Zend Framework 1.8)

Zend framework- AiTi conference [email protected] 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 [email protected] 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 thành phần cơ bản :1. Code library 2. Scripting language 3. API

Ngoài 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 [email protected] 4

Các chuẩn để xây dựng một Framework MVC: Có hỗ trợ Model – Control – View? Multiple DB's: Làm 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 [email protected] 5

Các framework phổ biến

Zend Cake CodeIgniter Doctrine Propel Spring .NET framework

Zend framework- AiTi conference [email protected] 6

Mô hình MVC

Zend framework- AiTi conference [email protected] 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 [email protected] 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ợ làm việc với Tempalate engine kết hợp cùng tầng View

Zend framework- AiTi conference [email protected] 9

Mô hình MVC cơ bản trong ứng dụng của Zend

Zend framework- AiTi conference [email protected] 10

Zend framework làm việc thế nào?

Các lớp được phân cấp theo tên thư mụcVới class Zend_Db_TableTương ứng chúng ta có cây thư mụcZend|_Db |_Table |_ class Zend_Db_Table.php

Zend framework- AiTi conference [email protected] 11

Zend framework làm việc thế nào?[1]

Controller thể hiện trên URLhttp://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 thihttp://localhost/zfdemo/admin/login: Tìm tới loginAction trong controller adminController

để thực thihttp://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 [email protected] 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 vào 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 [email protected] 13

Mô hình MVC của Zend

Zend framework- AiTi conference [email protected] 14

Cấu trúc thư mục của project

Zend framework- AiTi conference [email protected] 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 năng Debug, khai báo về múi giờ làm việc, …

2. Khai báo đường dẫn: Load các class làm 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 [email protected] 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 [email protected] 17

Zend_config

Chức năng: 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 [email protected] 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 [email protected] 19

Khai báo cấu hình & khởi tạoThiế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 Frameworkset_include_path('../library/'. PATH_SEPARATOR . '../application/models');

Gọi lớp Zend_Loaderinclude "Zend/Loader.php";

Gọi các lớp được sử dụngZend_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 [email protected] 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 toàn 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 [email protected] 21

Zend_Db

Chức năng: 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 này được sử dụng trong tầng Model và giao tiếp trực tiếp với Database

Zend framework- AiTi conference [email protected] 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 toàn cục $registry = Zend_Registry::getInstance();

$registry->set('db', $db);

Zend framework- AiTi conference [email protected] 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 [email protected] 24

Ví dụ về Model trên Zend_Db[1] Khai báo lớp trong Modelclass 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 [email protected] 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 hàm 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 [email protected] 26

Ví dụ về Zend Controller AdminController.php tập hợp các action xử lý trong phần admin .

Trong lớp này 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 này đượ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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 30

Demo ứng dụng sử dụng ZF tổ chứctheo mô hình MVC

Bài thuyết trình đến đây kết thúc

Chóc c¸c b¹n cã mét nam míi víi nhiÒu thµnh c«ng míi, trµn ®Çy niÒm vui vµ

h¹nh phóc

AITI-APTECH