• Ubuntuに関連したハード、ソフトの備忘録サイトです

「CodeIgniter徹底入門」の3.x系への対応(4)ー3.0RC2でも動作【追記】

CodeIgniter3.0はRC2(リリース候補2)が公開されていますが、2.x系に施した変更プラスアルファで「CodeIgniter徹底入門」の例題システム、①コンタクト・システム②掲示板システム③簡易ショッピングサイトが動作しました。

ハマったところは、①Validationクラスの使い方②Viewファイルの中でのViewファイルの呼び方で、2.x系列が打倒しないものがある③当然ですが、applicationディレクトリにあるlanguage/japaneseディレとクリに、systemディレクトリにあるものを間違えておくとどうにもならない画面になるーの三点でした。以下、順番に述べます。

【1】Validationクラスの使い方

Validationクラスは「徹底入門」では次のように記されています。

	// バリデーションの設定
	function _set_form_validation()
	{
		//return; 
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('
‘, ‘
');

		//$fields['name']  = '名前';
		//$fields['zip']   = '郵便番号';
		//$fields['addr']  = '住所';
		//$fields['tel']   = '電話番号';
		//$fields['email'] = 'メールアドレス';
		//$this->form_validation->set_fields($fields);

		//$rules['name']  = 'trim|required|max_length[64]';
		//$rules['zip']   = 'trim|valid_emailmax_lenght[8]';
		//$rules['addr']  = 'trim|required|max_length[128]';
		//$rules['tel']   = 'trim|required|max_length[20]';
		//$rules['email'] = 'trim|required|valid_email|max_lenght[64]';
		$this->form_validation->set_rules('name', '名前', 'trim|required|max_length[64]');
		$this->form_validation->set_rules('zip', '郵便番号', 'trim|required|max_lenght[8]');
		$this->form_validation->set_rules('addr', '住所', 'trim|required|max_length[128]');
		$this->form_validation->set_rules('tel', '電話番号', 'trim|required|max_length[20]');
		$this->form_validation->set_rules('email', 'メールアドレス', 'trim|required|valid_email|max_lenght[64]');
	}

この関数をconfirm関数から呼び出していますが、何故か、Validationに失敗しました。それで、次のように最も単純なやり方で条件を指定し、クラスを走らせると何とかうまく動作してくれたようです。

	// 注文内容確認
	function confirm()
	{
		//$this->_set_form_validation();
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('
‘, ‘
');

		//$this->form_validation->set_rules('name', '名前', 'trim|required|max_length[64]');
		//$this->form_validation->set_rules('zip', '郵便番号', 'trim|required|max_lenght[8]');
		//$this->form_validation->set_rules('addr', '住所', 'trim|required|max_length[128]');
		//$this->form_validation->set_rules('tel', '電話番号', 'trim|required|max_length[20]');
		//$this->form_validation->set_rules('email', 'メールアドレス', 'trim|required|valid_email|max_lenght[64]');
		$rule01 = array('field' => 'name', 'label' => '名前', 'rules' => 'trim|required|max_length[64]');
		$rule02 = array('field' => 'zip', 'label' => '郵便番号', 'rules' => 'trim|required|max_length[8]');
		$rule03 = array('field' => 'addr', 'label' => '住所', 'rules' => 'trim|required|max_length[64]');
		$rule04 = array('field' => 'tel', 'label' => '電話番号', 'rules' => 'trim|required|max_length[12]');
		$rule05 = array('field' => 'email', 'label' => 'メールアドレス', 'rules' => 'trim|required|valid_email|max_length[64]');

		$rule_sets = array($rule01, $rule02, $rule03, $rule04, $rule05);
		
		$this->form_validation->set_rules($rule_sets);
		
		if ($this->form_validation->run() == TRUE)
		{
# 検証をパスした入力データは、モデルを使って保存します。
			//$data['name']  = $this->form_validation->name;
			//$data['zip']   = $this->form_validation->zip;
			//$data['addr']  = $this->form_validation->addr;
			//$data['tel']   = $this->form_validation->tel;
			//$data['email'] = $this->form_validation->email;
			$data['name']  = $this->form_validation->set_value('name');
			$data['zip']   = $this->form_validation->set_value('zip');
			$data['addr']  = $this->form_validation->set_value('addr');
			$data['tel']   = $this->form_validation->set_value('tel');
			$data['email'] = $this->form_validation->set_value('email');
			$this->Shop_model->set_customer_info($data);

			$cart = $this->Shop_model->get_cart();
			$data['total'] = $cart['total'];
			$data['cart']  = $cart['items'];

# CSRF対策のワンタイムチケットを発行します。
			$this->ticket = md5(uniqid(mt_rand(), TRUE));
			$this->session->set_userdata('ticket', $this->ticket);

			$data['action'] = '注文内容の確認';
			$data['main']  = $this->load->view('shop_confirm', $data, TRUE);
			//$data['main']  = CI_loader::View('shop_confirm', $data, TRUE);
		}
		else
		{
			//echo 'Failure !!'; exit;
			$data['action'] = 'お客様情報の入力';
			$data['main']  = $this->load->view('shop_customer_info', '', TRUE);
			//$data['main']  = CI_Loader::View('shop_customer_info', '', TRUE);
		}

		$this->load->view('shop_tmpl_checkout', $data);
		//CI_loader::View('shop_tmpl_checkout', $data);
	}

【2】Viewファイルの中でのViewファイルの呼び方 次のようにする必要がありました。

 

[?php CI_Loader::View('ci_footer');?]

【3】application/language/japanese/pagination_lang.phpの中身

>?php 
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.2.4 or newer
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2014, British Columbia Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @package	CodeIgniter
 * @author	EllisLab Dev Team
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
 * @copyright	Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
 * @license	http://opensource.org/licenses/MIT	MIT License
 * @link	http://codeigniter.com
 * @since	Version 1.0.0
 * @filesource
 */
defined('BASEPATH') OR exit('No direct script access allowed');
$lang['pagination_first_link'] = '‹ 第一頁';
$lang['pagination_next_link'] = '-->';
$lang['pagination_prev_link'] = '<';
$lang['pagination_last_link'] = '最後一頁 ›';
/* End of file pagination_lang.php */
/* Location: ./application/language/traditional-chinese/pagination_lang.php */

ここんところ、間違ってsystem/language/english/のphpファイルを入れると、目も当てられない画面になります。あと、認証システムが動くかどうか調べてみます。

【補論】認証システムの動作について
SimpleLoginSecure.phpがそのままでは動作しませんでした。session->sess_create()関数が廃止されているためです。次のようにしたら、動作しました。

		if ($query->num_rows() > 0) 
		{
			$user_data = $query->row_array(); 

			$hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);

			if(!$hasher->CheckPassword($user_pass, $user_data['user_pass']))
				return false;

			//Destroy old session
			//$this->CI->session->sess_destroy();
			
			//Create a fresh, brand new session
			//$this->CI->session->sess_create();
			$this->CI->session->sess_regenerate(TRUE);

CodeIgniter3.0のマニュアルにあるsession->sess_generate()関数で、destroy, createを同時に行うように変更されたみたいです。

ということで、CodeIgniter3.x系列でも、Symfony1.x系列と2.x系列ほどの大きな変更はなされていない感じはします。最大の変更点は、ライセンス形態が商用にも向いているMITライセンスに変更され、開発元がへんこうされたことのように思われます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA