5

Click here to load reader

Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

Embed Size (px)

DESCRIPTION

Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7 Để tìm hiểu kỹ hơn các bạn hãy truy cập: Website: www.zend.vn Facebook: facebook.com/zendvngroup Youtube: youtube.com/user/luutruonghailan

Citation preview

Page 1: Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

Nhúng template vào ứng dung Zend Framework 2 (P3)

Xem 'Bài 7 - Nhúng template vào ứng dung ZF2 (P2)' trước khi thực hành bài này

D. Thay đổi thư mục chứa Template trong Zend Framework 2

- Trong các phần trên chúng ta thấy LAYOUT và các thư mục /css, /javascript và /images nằm ở 2 thư

mục khác nhau. Điều này làm mỗi lần chúng ta chỉnh sửa rất tốn thời gian tìm kiếm. Trong phần này

chúng ta sẽ gom LAYOUT và các thư mục css, javascript, images vào chung cùng một thư mục. Chúng ta

sẽ thực hiện như sau:

- Tạo thư mục /templates trong thư mục /public

- Tạo thư mục /hairstyle trong thư mục /public/templates

- Mở tập tin /public/define.php thêm vào đoạn mã để định nghĩa đường dẫn URL chứa template như

sau:

define('PUBLIC_PATH' , realpath(APPLICATION_PATH . '/public'));

define('TEMPLATE_PATH' , realpath(PUBLIC_PATH . '/templates'));

- Vậy tập tin /public/define.php có nội dung như sau:

<?php

//Đường dẫn đến thư mục chứa thư mục hiện thời

chdir(dirname(__DIR__));

//Hằng số lưu đường dẫn thư mục ứng dụng

//Hằng số lưu thông tin đường dẫn thư mục

define('APPLICATION_PATH', realpath(dirname(__DIR__)));

//Hằng số lưu đường dẫn thư mục chứa thư viên ZF2

define('LIBRARY_PATH', realpath(APPLICATION_PATH . '/library/'));

define('PUBLIC_PATH' , realpath(APPLICATION_PATH . '/public'));

Page 2: Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

define('TEMPLATE_PATH' , realpath(PUBLIC_PATH . '/templates'));

- Di chuyển các thư mục

/public/css

/public/fonts

/public/images

- Vào thư mục /public/templates/hairstyle

- Di chuyển tất cả các tập tin trong thư mục /module/Template/view/layout vào thư

mục /public/templates/hairstyle chúng ta sẽ được cấu trúc thư mục như sau:

- Bây giờ chúng ta sẽ chạy thử localhost:8000/zf2basic/public/template/

Page 3: Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

- Chúng ta thấy lỗi xảy ra vì vậy chúng ta cần phải thay đổi cấu hình để LAYOUT có thể hiển thị.

- Mở tập tin /module/Template/config/module.config.php tìm đoạn mã:

'view_manager' => array(

'template_map' => array(

'layout/home' => __DIR__ . '/../view/layout/index.phtml',

'layout/about' => __DIR__ . '/../view/layout/about.phtml',

'layout/news' => __DIR__ . '/../view/layout/news.phtml',

'layout/hairstyle' => __DIR__ . '/../view/layout/hairstyle.phtml',

'layout/contact' => __DIR__ . '/../view/layout/contact.phtml',

),

- Sửa lại như sau:

'view_manager' => array(

'template_map' => array(

'layout/home' => TEMPLATE_PATH . '/hairstyle/index.phtml'

),

- Mở tập tin /public/templates/hairstyle/index.phtml thêm vào đoạn mã sau vào đầu trang:

<?php

$templateURL = $this->basePath('/templates/hairstyle/');

?>

Tìm đoạn mã:

<link rel="stylesheet" href="<?php echo $this->basePath();?>/css/style.css" type="text/css">

Sửa lại thành:

<link rel="stylesheet" href="<?php echo $templateURL;?>css/style.css" type="text/css">

Page 4: Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

Tìm đoạn mã:

<?php echo $this->render("layout/menu.php");?>

Sửa lại thành:

<?php require_once 'menu.php';?>

Tìm đoạn mã:

<?php

if($this->layout()->getChildren()[0]->getTemplate() == 'template/index/index'){

echo $this->render("layout/banner.php");

}

?>

Sửa lại thành:

<?php

if($this->layout()->getChildren()[0]->getTemplate() == 'template/index/index'){

require_once 'banner.php';

}

?>

- Bây giờ chúng ta sẽ chạy thử localhost:8000/zf2basic/public/template/

- Chúng ta đã thấy giao diện đã hiển thị nhưng không load được hình chúng ta sẽ chỉnh lại đường dẫn.

- Mở tập tin /public/templates/hairstyle/banner.php sửa nội dung lại thành

<div class="article">

<img src="<?php echo $templateURL;?>images/great-hairstyle.jpg" alt="">

<h1>Great hairstyle comes from the experts.</h1>

</div>

Page 5: Giáo trình Zend Framework 2.0 - Nhúng template vào ứng dung ZF2 (P3) - Bài 7

- Mở tập tin /module/Template/view/template/index/index.phtml thêm dòng lệnh vào đầu tập tin:

<?php

$templateURL = $this->basePath('/templates/hairstyle/');

?>

Tìm các đoạn mã

<?php echo $this->basePath();?>

Sửa thành:

<?php echo $templateURL;?>

- Thực hiện điều này tương tự cho các tập tin VIEW: /module/Template/view/template/index/about.phtml

/module/Template/view/template/index/contact.phtml

/module/Template/view/template/index/hairstyle.phtml

/module/Template/view/template/index/news.phtml

- Sau đó chúng ta chạy thử các URL sau: localhost:8000/zf2basic/public/template/

localhost:8000/zf2basic/public/template/index/about

localhost:8000/zf2basic/public/template/index/contact

localhost:8000/zf2basic/public/template/index/hairstyle

localhost:8000/zf2basic/public/template/index/news

Download source here: http://www.zend.vn/download/pictures/zend-framework-2/04-nhung-templates-

vao-zf2/04-Nhung-templates-03.zip

Để tìm hiểu kỹ hơn các bạn hãy truy cập:

Website: www.zend.vn

Facebook: facebook.com/zendvngroup

Youtube: youtube.com/user/luutruonghailan