cakephp - the point of upgrade
TRANSCRIPT
CakePHPThe Point of Upgrade
1.3 2.xto@slywalker (Yasuo Harada)
and for 3.xfrom
about me
@slywalker
@slywalkerfollow me!
github.com/slywalker
github.com/slywalkerfork me!
TwitterBootstrap Plugingithub.com/slywalker/TwitterBootstrap
182 stars ★
TwitterBootstrap Plugingithub.com/slywalker/TwitterBootstrap
本日のお題
CakePHP移行ガイド
1.3 2.xから への
そして へ...3.x
book.cakephp.org/2.0/ja/appendices.html
ここを読んでもらったら終わってしまう話なんですが…
Cookbook
要点
Cookbookには書いていない経験から学んだ←をお届け
将来に備えて
一日にして成らず
これが基本だ
これが基本だ柔道で言えば受身
1.3 2.xから へ★ ディレクトリ構造、ファイル名およびクラス名の変更
★ __() → echo __()$html->link() → $this->Html->link()
★ 廃止されたグローバル関数や定数
★ cakeError → 例外処理
★ $this->data → $this->request->data
★ Componentクラスの継承元 Object → Component
★ Configure::read() → Configure::debug('debug')
結構これって面倒くさいですよねぇ…
そこはCakePHPのコアデベロッパーさすがです!
あるんです!
便利な道具が
あるんです!
便利な道具がUpgrade shell
まぁ、コアデベロッパー自身が多くのCakePHP製の案件を抱えてるでしょうから当然でしょう。
Usage:cake upgrade [subcommand] [-h] [-v] [-q]
Subcommands:
all Run all upgrade commands.tests Update tests class names to FooTest rather than FooTestCase.locations Move files and folders to their new homes.i18n Update the i18n translation method calls.helpers Update calls to helpers.basics Update removed basics functions to PHP native functions.request Update removed request access, and replace with $this->request.configure Update Configure::read() to Configure::read('debug')constants Replace Obsolete constantscomponents Update components to extend Component class.exceptions Replace use of cakeError with exceptions.
Console/cake upgrade -h
Upgrade
@ichikawayCon
sole/c
ake up
grade
all
詳しく載ってます!
だいたいのことは
shellこの人が書いたよ
会場には筆者の方々が来られています。
懇親会のときにグリグリ突っ込んだ質問を皆さんしてみましょう。
Upgrade shellでできないこと?
.htaccesscore.php
index.phpbootstrap.php
要書換
基本的にはコピペでいけます。
ここらへんのファイルをグリグリいじってしまった方はご愁傷様です。
特にややこしいことをしていないアプリケーションだったら、Upgrade shellとファイルのコピペで動くようになります。
すごいですね
ちなみに先ほどの「ややこしいこと」とは
SecurityComponentAuthComponentなど使っていると、要修正です。
このへんの修正は、Upgrade shellのあとエラーを潰していく感じです。
CookbookとかAPI見ながらとか…
一日にして成らずやっぱり手間やん…
その前に!
gradeUp
1.3本当に最新のコード?
良くも悪くも後方互換がしっかりしてる
知らずしらずのうちに時代遅れのコードを書いていることがよくあります。(だってそれで動くんだもの…)
$articles = $this->Article->find( 'all', array( 'conditions' => array( 'Article.status' => 'enable' ) )); 1.3 2.x
例えば find
1.3Model::find( $conditions = null, $fields = array(), $order = null, $recursive = null)
懐かしい引数たちです。
null,nullフレームワークと呼ばれていた頃の名残です。
2.xModel::find( $type = 'first', $query = array())
見事にバッサリとキレイになってますね。
メジャーバージョンが上がると非推奨の部分はこのように切り捨てられます。
Folder::mkdir()Folder::mv()Folder::ls()Folder::cp()Folder::rm()Set::isEqual()String::getInstance()CakeSession::del()
1.3非推奨
この他にも、引数が変更になったり新しいオプションが増えたりしています。
今まで面倒くさかったりそもそも、できなかったことがコッソリと簡単にできるようになっていることは、よくあることです。
1.3非推奨コードを全てなくす!
将来に備えて
来たるべき
deprecatedこれまでの説明でアップグレードの
重要な点がお解りいただけたでしょうか?
非推奨が出てくるタイミング
2.2.3 バグフィックス
機能の追加改良・非推奨の追加
PHPのバージョンサポート変更・非推奨のサポート停止
1.x系はグダグダでしたが2.x系はこのポリシーがしっかりと守られているようです。
すでに2.0の段階で非推奨になってしまったかわいそうなクラスもあります。
EmailComponent
CakeEmail
非推奨
CakeEmail<?phpApp::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();$email->template('welcome', 'fancy') ->emailFormat('both') ->to('[email protected]') ->from('[email protected]') ->send();
コンポーネント形式ではなく、ライブラリ形式で提供されるようになりました。
チェーンメッソドなモダンなつくりになっています。
ShellやModelの中でも使いやすくなりました。
2.xもうすぐ非推奨?
正式に非推奨のアナウンスはありませんが、今後そうなりそうなものもいくつか見受けられます。
非推奨か?RequestHandlerComponentCakeRequest
<?php$this->RequestHandler->isAjax();$this->request->is('ajax');$this->request->isAjax();
$this->RequestHandler->isPost();$this->request->is('post');$this->request->isPost();
$this->RequestHandler->isSSL();$this->request->is('ssl');$this->request->isSsl();
RequestHandlerでできることで、CakeRequestでできるものがあれば、今のうちに変更しておいたほうがいい。
RequestHandler自体はなくならないと思います。ちゃんと独立した役割を持ってるので。
Cake 超便利
Request$this->request
<?php// 1.x$this->params['controller'];// 2.x$this->request->controller;$this->request['controller'];$this->request->params['controller'];
CakeRequestオススメ
<?php// Passed arguments// 1.x$this->params['pass'];// 2.x$this->request->pass;$this->request['pass'];$this->request->params['pass'];
CakeRequest
オススメ
<?php// named parameters// 1.x$this->params['named'];// 2.x$this->request->named;$this->request['named'];$this->request->params['named'];
CakeRequest
オススメ
<?php// url is /posts/index?page=1&sort=title// 1.x$this->params['url']['page'];// 2.x$this->request['url']['page'];$this->request->query['page'];$foo = $this->request->query('not_exist');// $foo === null
CakeRequest
オススメだが、まだ実装されてない
<?php// 1.x$value = $this->data['Post']['title'];// 2.x$value = $this->request->data['Post']['title'];$value = $this->request->data('Post.title');$foo = $this->request->data('not_exist');// $foo === null
$this->request->data('Post.title', 'New post') ->data('Comment.1.author', 'Mark');
CakeRequest
メソッドが実装されているものはそれを使ったほうが後々よさそうな雰囲気です。
3.xはオブジェクトObjectしたフレームワークになりそうですね。
2.xも徐々にその方向で準備されていくと思います。
(業務連絡:
〆の話をお願いします。)
質問ありますか?
聴いて頂いてありがとうございました!