そろそろそれっぽいWebアプリを作ってみようと思って、一行掲示板にチャレンジしてみた。
ちなみに、前回のエントリー CodeIgniter | PCとモバイルのview切り替えでは、_output メソッドで携帯用に文字コードを変更してたけど、面倒くさいのでフックさせる方向で行きます。
まずは application/config/hooks.php に $hook['display_override']を追加します。これは、処理されたデータ(ページ)がブラウザに送信する時に呼び出されます。参考エントリー CodeIgniter リクエスト値を内部文字コードに変換処理
クラス名、ファイル名が変わっているので注意してください。
$hook['display_override'] = array(
'class' => 'ConvertEncoder',
'function' => 'toMobileEncode',
'filename' => 'ConvertEncoder.php',
'filepath' => 'hooks'
);
で、application/hooks/ConvertEncoder.php に toMobileEncode を追加します。
function toMobileEncode()
{
$CI =& get_instance();
$CI->load->library('user_agent');
$output = '';
if ($CI->agent->is_mobile())
{
$output = mb_convert_encoding($CI->output->get_output(), 'SJIS', $CI->config->item('charset'));
// モバイル用viewを用意してない時を考えて念のためmeta変換
$patt = "/(<meta http\-equiv=\"content\-type\" content=\"text\/html; charset=).*?\"(\s\/)*>/i";
$rep = "$1shift-jis\"$2>";
$output = preg_replace($patt, $rep, $output);
}
$CI->output->_display($output);
}
application/controllers に minibbs.phpを追加。
<?php
class Minibbs extends Controller
{
private $log_file = 'Youのフォルダ名にしちゃいなYO!';
private $log_count = 10;
function index()
{
$this->load->helper(array('file', 'html', 'url', 'form'));
$this->load->library('validation');
// バリデーション設定
$rules['message'] = "trim|required|max_length[20]";
$this->validation->set_rules($rules);
$fields['message'] = 'メッセージ';
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<div class="err">', '</div>');
// bbsデータ
$bbs_data = $this->_read();
// 検証エラーまたは初回アクセス時
if ($this->validation->run() == FALSE)
{
$bbs_data = $this->input->xss_clean(htmlspecialchars($bbs_data, ENT_QUOTES));
$bbs_data = explode("\n", $bbs_data);
$data = array();
$data['bbs'] = $bbs_data;
$this->load->view('common');
$this->load->view('minibbs', $data);
}
else
{
$date = date("H:i:s |", time());
$bbs_data = explode("\n", $bbs_data);
$bbs_data[] = $date ." ". $this->input->post('message');
if ($this->log_count < count($bbs_data))
{
array_shift($bbs_data);
}
$this->_write($bbs_data);
header('Location: '.$this->config->site_url().'minibbs.html');
}
}
function _read()
{
return trim(read_file($this->log_file));
}
function _write($str = null)
{
$str = (is_array($str)) ? implode("\n", $str) : null;
if(!write_file($this->log_file, $str))
{
show_error('データを書き込めませんでした。');
}
}
}
?>
そして、view。されど、view。面倒くさいからPC、携帯共用で。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="<?=site_url()?>css/minibbs.css" />
<title>一行掲示板</title>
</head>
<body>
<h1>一行掲示板</h1>
<p>CodeIgniter で作った一行掲示板</p>
<?php if ($this->validation->error_string):?>
<div id="err-box">
<?=$this->validation->error_string; ?>
</div>
<?php endif;?>
<div id="bbs">
<?=ol($bbs)?>
</div>
<?=form_open('minibbs')?>
<input type="text" name="message" value="<?=$this->validation->message?>" class="ipt-bg" /></dd>
<input type="submit" value="書き込み" />
<?=form_close()?>
<?=Common::menu()?>
</body>
</html>
スポンサーサイト