Đăng ký Đăng nhập

Tài liệu Cơ bản về Zend Framework

.PDF
30
65
125

Mô tả:

Cơ bản về Zend Framework
Zend Framework (Bài thuyết trình sử dụng Zend Framework 1.8) Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 1 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 2 https://fb.com/tailieudientucntt 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. 2. 3. Code library Scripting language 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 CuuDuongThanCong.com [email protected] 3 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 4 https://fb.com/tailieudientucntt Các framework phổ biến  Zend  Cake  CodeIgniter  Doctrine  Propel  Spring  .NET framework Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 5 https://fb.com/tailieudientucntt Mô hình MVC Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 6 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 7 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 8 https://fb.com/tailieudientucntt Mô hình MVC cơ bản trong ứng dụng của Zend Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 9 https://fb.com/tailieudientucntt 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 Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 10 https://fb.com/tailieudientucntt 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. Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 11 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 12 https://fb.com/tailieudientucntt Mô hình MVC của Zend Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 13 https://fb.com/tailieudientucntt Cấu trúc thư mục của project Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 14 https://fb.com/tailieudientucntt Khởi tạo ứng dụng trong Zend Framework  1. 2. 3. Để khởi tạo ứng dụng ZF chúng ta trải qua 3 bước cơ bản: 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, … Khai báo đường dẫn: Load các class làm việc của nhân ZF Thiết lập controllers: Chỉ ra nơi đáp ứng các request. Zend framework- AiTi conference CuuDuongThanCong.com [email protected] 15 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 16 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 17 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 18 https://fb.com/tailieudientucntt 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 [email protected] CuuDuongThanCong.com 19 https://fb.com/tailieudientucntt 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 CuuDuongThanCong.com [email protected] 20 https://fb.com/tailieudientucntt
- Xem thêm -

Tài liệu liên quan