Ini tutorial bikin blog simple pake Kohana

Diambil dari kohana-id@googlegroups.com

Blog nya ini simple banget.. cuma CR… bukan CRUD…
Semoga bisa sedikit membantu buat yang baru belajar Kohana..

Isi file application/controllers/welcome.php

<?php
class Welcome_Controller extends Template_Controller {
  public $template = 'template'; // View yang terdapat pada 'views/template.php'

  public function __construct() {
    parent::__construct();
    $this->template->menus = array('Home'=>'welcome',
                                   'Article'=>'welcome/article',
                                   'Post'=>'welcome/post');
  }
  public function index() {
    $this->template->content = 'Ini Home';
  }
  public function article () {
    $this->template->content = new View ('article');
    $db = ORM::factory('article');  $data = $db->orderby('sent','DESC')->find_all();
    $this->template->content->isi = $data;
  }
  public function post () {
    $this->template->content = new View ('post');
  }
  public function save () {
    $db = ORM::factory('article');
    $db->subject = $_POST['subject'];
    $db->message = $_POST['message'];
    $db->sent = date('Y-m-d H:i:s');
    $db->save();
    url::redirect('welcome/article');
  }
}

Isi file application/views/template.php

<html>
  <head>
  <title>Simple Blog with Kohana</title>
  <style>
  body {
    background: black;
    color: #fff;
    font-family: Arial, sans-serif;
    font-size: 14px;
  }
  a, a:visited {
    color: orange;
    text-decoration: none;
  }
  a:hover {
    color: gold;
  }
  #wrapper {
    width: 760px;
    margin: auto;
  }
  input, textarea {
    display: block;
  }
  .sent {
    color: #333;
  }
  #menu li {
    margin-right: 10px;
    display: inline;
  }
  .float-left {
    float: left;
  }
  .float-right {
    float: right;
  }
  .clear {
    clear: both;
  }
  .center {
    text-align: center;
  }
  </style>
</head>
<body>
  <div id="wrapper">
  <div id="header"><h1>Simple Blog with Kohana</h1></div>
  <ul id="menu">
    <?php foreach ( $menus as $menu => $url ): ?>
    <li><?php echo html::anchor($url, $menu) ?></li>
    <?php endforeach ?>
  </ul>
  <div id="content"><?php echo $content ?></div>
  <div id="footer">Copyright&copy; 2009 mul14</div>
  </div>
</body>
</html>

Isi file application/views/article.php

<?php foreach ($isi as $v): ?>
<h2><?php echo $v->subject ?></h2>
<div>Sent: <?php echo $v->sent ?></div>
<div><?php echo $v->message ?></div>
<?php endforeach ?>

Isi file application/views/post.php

<?php
echo form::open('welcome/save');
echo form::label('subject', 'Subject') . form::input('subject');
echo form::label('message', 'Message') . form::textarea('message');
echo form::submit('send', 'Send');
echo form::close();
?>

Isi file application/models/article.php

<?php
class Article_Model extends ORM {}

MySQL nya:

CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject` tinytext CHARACTER SET utf8 NOT NULL,
`message` text CHARACTER SET utf8 NOT NULL,
`sent` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
INSERT INTO `articles` (`id`, `subject`, `message`, `sent`) VALUES
(1, 'Vestibulum hendrerit', '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas nisi. Praesent pharetra augue quis magna. Nulla eros. Donec quis urna in lorem nonummy iaculis. Suspendisse tempor venenatis mauris. Cras pulvinar purus id leo posuere imperdiet. Duis aliquam elementum ipsum. Proin non leo. Aliquam erat volutpat. Etiam ultricies pede eget nulla.</p>\r\n', '2009-06-28 03:15:16');

Jangan lupa sesuaikan isi application/config/config.php

5 Komentar

  1. Adiputra berkata:

    blum coba ke Kohana..tapi sepertinya menari juga..

    kira2 untuk pemula..enak pakai CI ato KOHANA ?

    salam kenal sebelumnya..

  2. mul14 berkata:

    Yoo.. salam kenal juga.

    Hmm… sebenarnya CI dan Kohana 2.x gak jauh berbeda. Jadi kalau mempelajari salah satu, seharusnya bisa yg lainnya.

    Jangan lupa gabung di http://framework.web.id mas. Kalau ada pertanyaan silahkan ditanyakan disana… 😀

  3. monyetaink berkata:

    Wah info bagus,bisa di coba tuh…
    thanks infonya..

Tinggalkan Balasan ke monyetaink Batalkan balasan