CodeIgniter2.0.2でblogのチュートリアルを作る。

2.x.xのチュートリアルがなかったので、
1.x.x系のとかを参考にやってみました。


1. 公式からCodeIgniterをダウンロード

http://codeigniter.com/
トップページ右側の Download CodeIgniter Reactor を押下

2. 展開して使えるサーバーに上げる

適当に。

3. 多分エラーが出るので以下を直す

ルートのindex.phpの$application_folder

before >> $application_folder = "application";
after  >> $application_folder = dirname(__FILE__)."/application";

/application/config/config.php

before >> $config['uri_protocol'] = 'AUTO';
after  >> $config['uri_protocol'] = 'PATH_INFO';

エラーが出ない場合は気にせずそのまま
(ドキュメントルート以外にディレクトリを切って設置した場合は上記対応が必要になるっぽい)

4. テーブルとデータを作成

create table entry(
id int not null auto_increment,
title varchar(128),
body text,
primary key(id)
);
create table comments(
id int not null auto_increment,
entry_id int,
body text,
author varchar(100),
primary key(id)
);
insert into entry values(1,'test title1','test body1');
insert into entry values(2,'test title2','test body2');

5. DBに自動で接続する設定

/application/config/autoload.php

$autoload['libraries'] = array('database');

↑ここはデータベース名などではなく単純にdatabaseとかく

6. 接続先を設定

接続先の設定
/application/config/database.php


7. コントローラーを作る

/application/controllers/blog.php

<?php
class Blog extends CI_Controller {

  function __construct(){
      parent::__construct();

      //ヘルパの追加
      $this->load->helper('url');
      $this->load->helper('form');
  }

  function index()
  {
      $data['title'] = "My Real Title";
      $data['heading'] = "My Real Heading";

      $data['query'] = $this->db->get('entry');	//select * from entry;

      $this->load->view('blog_view', $data);
  }

  function comments()
  {
      $data['title'] = "comment Title";
      $data['heading'] = "comment Heading";

      $this->db->where('entry_id',$this->uri->segment(3));
      $data['query'] = $this->db->get('comments');

      $this->load->view('comment_view', $data);
  }

  function comment_insert()
  {
      $this->db->insert('comments',$_POST);

      redirect('blog/comments/'.$_POST['entry_id']);
  }
}
?>

8. viewを作る

/application/views/blog_view.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8" />
  <title><?php echo $title;?></title>
</head>
<body>
  <h1><?php echo $heading;?></h1>
  <table>
    <tr>
      <th>タイトル</th>
      <th>内容</th>
      <th>コメント</th>
    </tr>
    <?php foreach($query->result() as $row): ?>
    <tr>
      <td><?=$row->title ?></td>
      <td><?=$row->body ?></td>
      <td><?=anchor('blog/comments/'.$row->id,'Comments'); ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
</body>
</html>

/application/views/comment_view.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8" />
  <title><?php echo $title;?></title>
</head>
<body>
  <h1><?php echo $heading;?></h1>
  <?php if ($query->num_rows() > 0 ): ?>
  <table>
    <tr>
      <th>コメント人</th>
      <th>コメント</th>
    </tr>
    <?php foreach($query->result() as $row): ?>
    <tr>
      <td><?=$row->author ?></td>
      <td><?=$row->body ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
  <?php endif; ?>

  <p><?=anchor('blog','back to blog'); ?></p>

  <table>
    <?=form_open('blog/comment_insert'); ?>
    <?=form_hidden('entry_id',$this->uri->segment(3)); ?>
    <tr>
      <td>コメント人</td><td><input type="text" name="author"></td>
    </tr>
    <tr>
      <td>コメントの内容</td>
      <td><textarea name="body" rows="10"></textarea></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" value="こめんと"></td>
    </tr>
  </table>
</body>
</html>

9. ルーティングの設定をする

/application/config/routes.php

$route['default_controller'] = "Blog";