[KOHANA] Bài 7 (tutorial) kết hợp Database + Controller Template + View + Validation

Để minh họa sự kết hợp của những thành phần database+controller template +view tôi sẽ lấy ví dụ về ứng dụng web tin tức ở mức độ đơn giản.
Với một web tin tức có 3 bản article (nôi dung các tin )+ user(người post tin)+category (loại tin tức). Vì để minh họa nên tôi chỉ tạo CSDL ở mức độ cơ bản để bạn dễ nắm bắt.... 

CSDL http://www.mediafire.com/?had5t7yhyyj7lry

 PHP Code:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `tintuc_db` -- CREATE DATABASE `tintuc_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; USE `tintuc_db`; -- -------------------------------------------------------- -- -- Table structure for table `article` -- CREATE TABLE IF NOT EXISTS `article` ( `article_id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `content` text COLLATE utf8_unicode_ci NOT NULL, `post_userid` int(11) NOT NULL, `category` int(11) NOT NULL, `post_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`article_id`), KEY `article_id` (`article_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ; -- -- Dumping data for table `article` -- INSERT INTO `article` (`article_id`, `title`, `content`, `post_userid`, `category`, `post_time`) VALUES (1, 'bài viết thứ nhất', 'nội dung bài viết thứ 1', 1, 1, '2013-08-08 14:05:51'), (2, 'bài viết thứ hai ', 'nội dung bài viết thứ 2', 1, 1, '2013-08-08 14:05:51'), (3, 'Honda city', 'Honda city mới vừa ra mắt tháng 6/2013', 2, 3, '2013-08-08 14:18:08'), (4, 'có bỏ phạt xe chính chú?', 'có thông tin là tạm dừng xe chính chủ', 1, 3, '2013-08-08 14:18:08'); -- -------------------------------------------------------- -- -- Table structure for table `category` -- CREATE TABLE IF NOT EXISTS `category` ( `category_id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `note` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`category_id`), KEY `category_id` (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ; -- -- Dumping data for table `category` -- INSERT INTO `category` (`category_id`, `title`, `note`) VALUES (1, 'kinh tế', 'tin tức về kinh tế'), (2, 'thể thao', 'tin tức về thể thao'), (3, 'xe và đời sống', 'tin tức về ôtô xe máy'), (4, 'Vui Cười', 'truyện cười các loại'); -- -------------------------------------------------------- -- -- Table structure for table `users` -- CREATE TABLE IF NOT EXISTS `users` ( `user_id` int(11) NOT NULL, `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, KEY `user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- Dumping data for table `users` -- INSERT INTO `users` (`user_id`, `username`, `password`) VALUES (1, 'admin', 'fsgljagjkgl'), (2, 'dantri', 'fgajkljfalf');

và nhớ chỉnh trong database.php  trong config lại để đường dẫn đúng trong cSDL là tintuc_db

1. Đầu tiên ta thực hiện người sử dụng sẽ xem hết  danh sách các tin tức. để làm điều này cần  

ta tạo controller article.php đầu tiên  gửi thông tin ra template index.php trong thư mục article sẽ nhận dữ liệu, như sau 

 <?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Article extends Controller_Template {

    public function action_index() { 

     public $template 'article/index'

         $query = DB::select() 

                        ->from('article') 

                        ->join('users', 'left')->on('article.post_userid', '=', 'users.user_id') 

                        ->execute()->as_array(NULL); 

        $this->template->content = View::factory('user/newslist'); 

        $this->template->content->set(array( 

            'data' => $query 

        )); 

    }  

       }

}

// End Article

 Bây giờ ta vào thư mục view tạo thư mục article cho nó, và vào thư mục article tạo file index.php cho nó. Với nội dung

PHP Code:
<h1>Các tin mới nhất ^^!</h1> 

<div> 
    <?php echo $content ?> 
</div>

Ở đây ta sẽ xuất thông tin content từ controller  ra, Tuy nhiên content này được lấy từ View newslist trong thử mục user và dữ liệu data được gửi ra để thể hiện cho view này. Vì vậy, bên trong thư mục view tạo một thư mục user để dùng thể hiện những chức năng của user và tạo tập tin newslist.php theo dòng lấy nội dung của view newslist ở trên

   $this->template->content = View::factory('user/newslist'); 

 Newslist.php cơ bản chỉ là một bảng danh sách các tin tức  với nội dung được trình bày từ biến data như sau 

<?php 

if ($data != false) { //KIEM TRA DATA CO HAY KHÔNG 

    ?> 

    <table width="100%" border="1" cellspacing="2" cellpadding="3"> 

        <tr style="background-color: #CCC"> 

            <td>title</td>  <td>content</td> <td>post time</td>  <td>user Post</td> 

        </tr> 

        <?php 

        foreach ($data as $dt) { //FOREACH DATA RA 

            ?> 

            <tr> 

                <td><?php echo $dt['title']?></td> <td><?php echo $dt['content']?></td>   <td><?php echo $dt['post_time']?></td> <td><?php echo $dt['username']?></td> 

            </tr> 

        <?php } ?> 

    </table> 

<?php } ?>

rồi bây giờ chạy thử http://localhost/kohana/article xem thế nào nhé....

(còn tiếp) bài sau sẽ trình bày tiếp về chức năng của admin