みずの日記

ボカロとかニコ動とか忘備録とか

MY_Controller拡張

今よく使う設定を書き出しておくことにします。
影響を受けまくったFrameWork:CakePHPRuby on Rails

想定している使い方。
$this->put_layout($this->view('aaa', $data, true));

<?php

if (!defined('BASEPATH'))	exit('No direct script access allowed');

/**
 * MY_Controller
 *
 * @author localdisk
 *
 * @property CI_Loader $load
 * @property CI_Input $input
 * @property CI_Session $session
 * @property CI_Config $config
 * @property CI_Form_validation $form_validation
 * @property CI_URI $uri
 *
 * @property Mailer $mailer
 * @property Tank_auth $tank_auth
 *
 */
class MY_Controller extends CI_Controller
{

	private $layout = '', $title = '';

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

		//DB初期化
		//デバッグ時はdevelopmentを読み込む
		$this->load->database(ENVIRONMENT);

		//共通ヘルパー
		$this->load->helper('url');
		$this->load->helper('debuglib');
		$this->load->helper('kado');
		$this->load->helper('cookie');

		//共通ライブラリ
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		$this->load->library('form_validation');

		//共通モデル
		$this->model('users', 'user'); //usersは予約済み

	}

	/**
	 * 読み込み拡張
	 * メソッドがあるか調べて、あるならそのメソッドを呼び出す。
	 * 無いならビューを呼び出す。
	 * ../index.php/aaa/bbb/ なら
	 * ./application/views/aaa/bbb_view.php を呼び出す。
	 * 無いなら404だ!
	 * @param type $method
	 * @param type $params 
	 */
	function _remap($method, $params = array()){
		if(method_exists($this, $method)){
			call_user_func_array(array($this, $method), $params);
		} else {
			//ビューを探す
			$view_file = FCPATH.APPPATH.'views/' .strtolower(get_class($this)).'/'.$method.'_view.php';
			if(file_exists($view_file)){
				$output = $this->view(strtolower(get_class($this)).'/'.$method, NULL, true);
				$this->put_layout($output);
			} else {
				show_404();
			}
		}
	}

	/**
	 * テンプレートレイアウトを指定する。
	 * 毎回、全部指定するのは面倒なので。
	 * 呼び出しテンプレートの指定が無ければ _default を呼び出す。
	 * @param type $data はめ込むHTML文章
	 * @param type $layout レイアウト指定
	 * @param type $title  Header Titleを指定する。
	 */
	protected function put_layout($data, $layout = NULL, $title = NULL){

		//レイアウト指定
		if ($layout != null )
			$this->layout = $layout;

		//タイトル指定
		if ($title != null )
			$this->title = $title;

		//指定されてなければデフォルト
		if ($this->layout == '')
			$this->layout = '_default';

		//Titleがセットされているか
		if($this->title != '')
			$this->title .= " - ";

		$this->title .= "";

		//データがStringならArrayに
		if (is_string($data))
			$output['data'] = $data;

		//print_a($output);
		$output['title'] = $this->title;
		$this->view('layout/'.$this->layout, $output);
	}

	/**
	 * Header Titleの指定
	 * @param type $name 
	 */
	protected function set_title_name($name){
		$this->title = $name;
	}

	/**
	 * テンプレートレイアウトの指定
	 * @param string $layout
	 */
	protected function set_layout_name($layout){
		$this->layout = $layout;
	}

	/**
	* Model 呼び出しのラッパー
	* @param  string $name
	* @return MY_Model
	* @link	http://elliotscode.com/2011/03/28/codeigniter-my-controller
	*/
	protected function model($name, $su_name = false)
	{
		if( $su_name === false ){
			$su_name = $name;
		}
		$name = $name . '_model';
		if( ! isset($this->{$name}))
		{
			$this->load->model($name, $su_name);
		}
		return $this->{$su_name};

	}

	/**
	 * View呼び出しのラッパー
	 * @param string $name
	 * @param array $data
	 * @param boolean $return
	 * @return type
	 */
	protected function view($name, $data = array(), $return = FALSE)
	{

		$view_file = $name. '_view.html';

		if(file_exists(FCPATH.APPPATH.'views/' .$view_file)){
			return $this->load->view($view_file, $data, $return);
		}

		$view_file = $name. '_view.php';

		if(file_exists(FCPATH.APPPATH.'views/' .$view_file)){
			return $this->load->view($view_file, $data, $return);
		}

		show_404();

	}

}