10分で分かる
最近のCakePHP
 2012/10/12 shin1x1
  第4回関西PHP勉強会
@shin1x1


 • 新原 雅司(しんばら まさし)
 • 「Shin x blog」 http://www.1x1.jp/blog/




                         (c) 2012 Masashi Shinbara @shin1x1
[告知] CakePHP2実践入門



               好評御礼!

           2012/09/29 発売




            (c) 2012 Masashi Shinbara @shin1x1
Agenda




 • 今のCakePHPは?
 • 新機能は?
 • 次のCakePHPは?



                 (c) 2012 Masashi Shinbara @shin1x1
今のCakePHPは?
 • 2011/10 2.0.0リリース、2.0.1リリース

 • 2011/11 2.0.2リリース、2.0.3リリース、2.0.4リリース

 • 2012/03 2.1.0リリース、2.1.1リリース

 • 2012/04 2.1.2リリース

 • 2012/05 2.1.3リリース

 • 2012/07 2.2.0リリース、2.1.5&2.2.1リリース

 • 2012/09 2.2.2リリース

 • 2012/10 2.2.3リリース

                          (c) 2012 Masashi Shinbara @shin1x1
今のCakePHPは?

 • CakePHP2.2.3が最新版
 • PHP5.2.8以上(例外、PDO etc)
 • PSR-0に合わせたパス構造
 • フレームワークをリファクタリング
 • lazy loadingによるパフォーマンス改善
 • PHPUnit
               (c) 2012 Masashi Shinbara @shin1x1
いまどきのコントローラ
app/Controller/PostsController.php
<?php
App::uses('AppController', 'Controller');

class PostsController extends AppController {
  public $uses = array('Post');

    public function add() {
      if ($this->request->is('post')) {
        if ($this->Post->save($this->request->data)) {
          $this->Session->setFlash('Your post has been saved.');
          $this->redirect(array('action' => 'index'));
        } else {
          $this->Session->setFlash('Unable to add your post.');
        }
      }
    }
}




                                        (c) 2012 Masashi Shinbara @shin1x1
2.1,2.2の新機能



 • ビューブロック、継承
 • Event System
 • ModelValidator
 • Hash


                    (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承




 • Twigライクなビューブロック、継承
 • 2.1から登場




             (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承




             (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承

   ビューブロック




             (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承




    継承



             (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承
 親テンプレート: レイアウトを決定
<h1><?php echo $this->fetch('title'); ?></h1>

<?php echo $this->fetch('content'); ?>
                                             ブロックを出力
<div class="actions">
    <h3>サイドバー</h3>
    <ul>
    <?php echo $this->fetch('sidebar'); ?>
    </ul>
</div>




                                 (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承
 子テンプレート: ブロックの値を設定
<?php $this->extend('/Common/view'); ?>
                                                      継承
<?php $this->start('title'); ?>
ビュー継承テスト
<?php $this->end(); ?>


<?php $this->start('sidebar'); ?>
<li>サイドバー1</li>
<li>サイドバー2</li>
<?php $this->end(); ?>

ここはcontent




                                    (c) 2012 Masashi Shinbara @shin1x1
ビューブロック、継承
 子テンプレート: ブロックの値を設定
<?php $this->extend('/Common/view'); ?>
                                                      継承
<?php $this->start('title'); ?>
ビュー継承テスト
<?php $this->end(); ?>
                                                ブロックの値

<?php $this->start('sidebar'); ?>
<li>サイドバー1</li>
<li>サイドバー2</li>
<?php $this->end(); ?>

ここはcontent




                                    (c) 2012 Masashi Shinbara @shin1x1
Event System

 • 柔軟なイベント通知、リスナー実装
 • Observerパターン
 • コールバックメソッド呼び出し
 • 2.1から登場
 • Dispatcher Filters(2.2から)

                  (c) 2012 Masashi Shinbara @shin1x1
Event System

•2.1未満




               (c) 2012 Masashi Shinbara @shin1x1
Event System

•2.1以降




               (c) 2012 Masashi Shinbara @shin1x1
Event System
 lib/Cake/Controller/Controller.php

•リスナーをアタッチ
<?php
$this->_eventManager = new CakeEventManager();
$this->_eventManager->attach($this->Components);
$this->_eventManager->attach($this);


•イベント通知
<?php
$this->getEventManager()
      ->dispatch(new CakeEvent('Controller.initialize', $this));
$this->getEventManager()
      ->dispatch(new CakeEvent('Controller.startup', $this));



                                 (c) 2012 Masashi Shinbara @shin1x1
Model Validator


 • Modelからバリデーション機能を分離
 • 動的にバリデーションの追加、削除
 • 独自バリデータの実装も可能
 • 2.2から登場


                  (c) 2012 Masashi Shinbara @shin1x1
Model Validator

•バリデーションルールの追加
<?php
$this->validator()->add('password', 'required', array(
  'rule' => 'notEmpty',
  'required' => 'create'
));



•バリデーションルールの削除
<?php
$this->validator()->remove('password');




                                 (c) 2012 Masashi Shinbara @shin1x1
Hashクラス



 • Set クラスが Hash クラスへ
 • Set クラスはCakePHP3では廃止
 • 2.2から登場



               (c) 2012 Masashi Shinbara @shin1x1
次のCakePHP



 • 2.2.4が11月予定
 • 2.3ブランチも開発中(新機能はこちら)
 • CakePHP3を発表



                 (c) 2012 Masashi Shinbara @shin1x1
CakePHP3

 • PHP 5.4 以上
 • namespace(PSR-0/1 対応)
 • traitsの利用
 • Modelがオブジェクトを返す
 • 2013/N 登場?

                (c) 2012 Masashi Shinbara @shin1x1
まとめ




 • これから使うなら最新版を(2.2.3)
 • Cookbookを参考に
 • もっとCakePHP2を知りたい人は。。。



              (c) 2012 Masashi Shinbara @shin1x1
ご清聴ありがとうございました!




          (c) 2012 Masashi Shinbara @shin1x1

10分で分かる最近のCakePHP

Editor's Notes