zend framework tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... ·...

30
1 Bài giảng Zend Framework 1.8

Upload: letruc

Post on 21-Mar-2018

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

1

Bài giảngZend Framework 1.8

Page 2: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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

Page 3: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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.

Page 4: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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?

Page 5: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

5

Các framework phổ biến

Zend Cake CodeIgniter Doctrine Propel Spring .NET framework

Page 6: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

6

Mô hình MVC

Page 7: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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ị

Page 8: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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

Page 9: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

9

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

Page 10: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

10

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

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

Page 11: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

11

Zend framework làm việc thế nào?[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.

Page 12: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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

Page 13: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

13

Mô hình MVC của Zend

Page 14: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

14

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

Page 15: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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.

Page 16: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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

Page 17: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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”

Page 18: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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');

Page 19: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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');

Page 20: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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();

Page 21: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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

Page 22: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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);

Page 23: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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();

Page 24: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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; }

}

Page 25: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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.

Page 26: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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)

Page 27: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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 ?>

Page 28: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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>

Page 29: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

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();

Page 30: Zend Framework Tutorialdulieu.tailieuhoctap.vn/books/cong-nghe-thong-tin/lap-trinh-web/... · Thiết lập controllers: ... Truy cập biến trong view sử dụng con trỏ $this

30

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

Bài giảng đến đây kết thúc

AITI-APTECH