2013年11月1日金曜日

CodeIgniterでGET, POSTの動作を分ける

FuelPHPで最初に便利だったのがhttpdのメソッドでコントローラーのアクションを分岐できるところ。
FuelPHPが動かないサーバの案件でCodeIgniterを使ったときに真っ先に追加したのがこの機能。

function action_xxx (){
function get_xxx (){
function post_xxx (){

と書くだけで、GET, POSTによるアクションの分岐が可能になります。


以下の内容のMy_Controller.phpを作成。

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

class MY_Controller extends CI_Controller
{
var $uploaded;
var $view_headers;
var $view_footers;
public function __construct()
{
parent::__construct();
}
// action_, post_, get_への振り分け
public function _remap($method, $params = array())
{
if ($method == '') $method = 'index';
if (strtolower($this->input->server('REQUEST_METHOD')) == 'post')
{
// POST
if (method_exists($this, 'post_'.$method))
{
return call_user_func_array(array($this, 'post_'.$method), $params);
}
elseif (method_exists($this, 'action_'.$method))
{
return call_user_func_array(array($this, 'action_'.$method), $params);
}
else
{
show_404();
}
}
else
{
// GET
if (method_exists($this, 'get_'.$method))
{
return call_user_func_array(array($this, 'get_'.$method), $params);
}
elseif (method_exists($this, 'action_'.$method))
{
return call_user_func_array(array($this, 'action_'.$method), $params);
}
else
{
show_404();
}
}
}
public function view($viewfile, $data)
{
foreach ($this->view_headers AS $header_file)
{
$this->load->view($header_file, $data);
}
$this->load->view($viewfile, $data);
foreach ($this->view_footers AS $footer_file)
{
$this->load->view($footer_file, $data);
}
}
}

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'AUTH_Controller.php';
?>

2013年10月19日土曜日

CodeIgniterのFormヘルパーのバグ(form_prep())

最近はFuelを使っていたのだがFuelの動作要件を満たさないサーバに当たってしまったので久々にCodeIgniter (2.1.4)を利用。

なんだけども、CodeIgniterのFormヘルパーにバグがあって困った。

Formヘルパーの各種関数を使うとvalueに設定する値は文字参照に変換してくれるのだが、同じname属性のものを2回使うと2回目は文字参照に変換してくれない。

これ↓を参考にform_helper.phpを拡張するMY_form_helper.phpを作成してみた。
https://bitbucket.org/matsuu/codeigniter-reactor/commits/20ca07f73bc3/

先人に感謝
MY_form_helper.php
function form_prep($str = '', $field_name = '')
{
static $prepped_fields = array();
// if the field name is an array we do this recursively
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = form_prep($val);
}
return $str;
}
if ($str === '')
{
return '';
}
// we've already prepped a field with this name
// @todo need to figure out a way to namespace this so
// that we know the *exact* field and not just one with
// the same name
if (isset($prepped_fields[$field_name]) && $prepped_fields[$field_name] == $str)
{
return $str;
}
$str = htmlspecialchars($str);
// In case htmlspecialchars misses these.
$str = str_replace(array("'", '"'), array("'", """), $str);
if ($field_name != '')
{
$prepped_fields[$field_name] = $prepped_fields[$field_name] = $str;;
}
return $str;
}
/* End of file MY_form_helper.php */
/* Location: ./application/helper/MY_form_helper.php */

2013年8月1日木曜日

Adobe Readerの旧バージョン

Adobe Readerの旧バージョンがほしい時、Adobeのサイトでダウンロードできる。

http://get.adobe.com/jp/reader/otherversions/

ただし現時点でバージョン9.5以降。

もっと古いのがほしい場合、FTPでダウンロードできる。

ftp://ftp.adobe.com/pub/adobe/reader/

ダウンロードマネージャ版ではなく、完全なインストーラー版をダウンロードしないと古いバージョンはインストールできなさそうなので注意が必要。

oldversion.comとかにもあったんだけど英語版のみで、日本語のPDFファイルでうまく開かないものがあったりとかして日本語バージョンが必要だったんです。上のサイトなら各種言語版があります。

急遽旧バージョンが必要になって探しました。あって助かった。

windowsのコマンドプロンプトを開く

ここに紹介されているコマンドプロンプトの開き方がイイ!
パスを引き継げる。便利!

http://www.lifehacker.jp/2013/03/130320windows_cmd.html

エクスプローラーのアドレスバーに、cmd[Enter] 

2013年7月10日水曜日

CentOS6でyumでちゃっちゃとtomcat6

tomcatを動かして以前のwarファイルを動かさないといけなくなったんですがやり方を忘れてて手間取ったのでメモ。

# yum install tomcat6
# yum install tomcat6-webapps
# yum install tomcat6-admin-webapps

manager用のユーザを追加する
# vi /etc/tomcat6/tomcat-users.xml
以下を追加


# service tomcat6 start
# chkconfig tomcat6 on

iptableとかでポート閉じてたら設定変更して開く。

ブラウザでアクセス
http://XXX.XXX.XXX.XX:8080/
tomcatの画面が出ればOK
そのままTomcat Managerに行ってwarファイルをデプロイする。

が、デプロイ失敗。
JVMバージョンが1.5なのが問題の気がするので上げてやる。

# yum -y install java-1.6.0-openjdk
# service tomcat6 restart

動かしたいwarファイルのデプロイが成功してました。
以上

2013年5月3日金曜日

PHPのコンパイル設定

今使っているPHPのフレームワーク、FuelPHP (ver 1.5)。
むっちゃ使いやすい。これは広がってほしい。

ただpdoじゃないmysqlドライバしか入っていないサーバに入れてmysqlドライバで動かそうとしたんだけど設定をどう書いてもpdoドライバで動かそうとしてPDO_Exceptionが出る。

phpとapacheを独自にコンパイルしているサーバ(別の方がセッティング)なので仕方なしにphp(ver. 5.4.12)をコンパイルしなおしました。phpをコンパイルするのなんて何年ぶりだろう。
あまりに久しぶりでオプションとかぜんぜん忘れてて、調べなおすのに半日かかりましたw

有効にしたモジュールは、
mysql関連
gd
mbstring
zip
※ PHP5.4からmysqlndがデフォルトで、--with-mysql-sock以外は値を指定しなくても大丈夫
※ fileinfo, session, json, xml関連などはデフォルトで入るので特に指定しない
※ mcrypt(fuelphpで必要)は面倒なのでFuelPHP内でPHPSecLibで代用してもらう


足りてない.hファイルをyumで入れる(環境によっては他にも必要なのが出てくるでしょう)


# yum install libjpeg-devel
# yum install libpng-devel
# yum install freetype-devel

configureする
# ./configure --prefix=/usr/local/php-5.4.12 --with-apxs2=/usr/local/apache_2.4.4/bin/apxs --with-gd --with-jpeg-dir=/usr/lib64 --with-png-dir=/usr/lib64 --with-zlib-dir=/lib64 --with-freetype-dir=/usr --enable-gd-native-ttf --with-mysql --with-mysqli --with-pdo-mysql --with-mysql-sock=/var/lib/mysql/mysql.sock --enable-mbstring --with-zlib --enable-zip


make, make testすると既知のバグっぽいのが出ますが目を瞑ってmake install。

apache再起動でめでたくPDOで動きました。

mysqlドライバで動かす書き方をちゃんと見つけたほうが良かった気がする。

2013年3月15日金曜日

chmod の小技


chmod -R では大雑把過ぎて使いづらい。
いい小技を忘れないうちにメモ。

カレントディレクトリ以下のディレクトリのみ変更
find ./ -type d -print | xargs chmod 775

カレントディレクトリ以下のファイルのみ変更
find ./ -type f -print | xargs chmod 664

HTMLファイルのみ
find ./ -name *.rhtml -type f -print | xargs chmod 644