Más contenido relacionado
La actualidad más candente (18)
Similar a 3時間濃縮CakePHP2.1 in PHPカンファレンス北海道2012 (20)
3時間濃縮CakePHP2.1 in PHPカンファレンス北海道2012
- 1. 3時間濃縮
CakePHP2.1
by nipotan
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 2. 本日の主旨
8時間耐久を圧縮
CakePHP2.1の基本
骨の髄に(強制的に)刻みこむ
時間が許す範囲で応用技
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 4. ?
CakePHP2実践入門(仮)
技術評論社より今夏出版予定
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 5. 本日のメニュー
MVCの考え方
セットアップの肝
コントローラーの使い方
モデルの使い方
ビューの使い方
応用技の紹介
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 6. by crabchick
MVCの考え方
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 8. CakePHPもMVC
Railsインスパイア系
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 9. なぜMVC
複雑化するプログラム
整理が必要
分割の経験則
Model View Controller
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 13. <?php
$keyword = '';
if (isset($_POST['keyword'])) {
$keyword = $_POST['keyword'];
}
mysql_connect('localhost','user','password');
mysql_select_db('cakephp_sample');
mysql_query('set names utf8');
$sql = sprintf(
"SELECT id,name,description FROM friends WHERE name LIKE '%s'",
mysql_real_escape_string('%'.$keyword.'%')
);
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
?>
<html>
<head><title>テストページ</title></head>
<body>
<form action="list1.php" method="POST">
<input name="keyword"><input type="submit">
</form>
<ul>
<?php
foreach ($data as $row) {
echo '<li>';
echo $row['name'];
echo $row['description'];
echo '</li>';
}
?></ul>
</body></html>
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 14. <?php
$keyword = '';
if (isset($_POST['keyword'])) {
$keyword = $_POST['keyword'];
}
mysql_connect('localhost','user','password');
mysql_select_db('cakephp_sample');
mysql_query('set names utf8');
$sql = sprintf(
"SELECT id,name,description FROM friends WHERE name LIKE '%s'",
mysql_real_escape_string('%'.$keyword.'%')
);
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
Modelへ
$data[] = $row;
}
?>
<html>
<head><title>テストページ</title></head>
<body>
<form action="list1.php" method="POST">
<input name="keyword"><input type="submit">
</form>
<ul>
<?php
foreach ($data as $row) {
echo '<li>';
echo $row['name'];
echo $row['description'];
echo '</li>';
}
?></ul>
</body></html> Viewへ
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 15. <?php
//model.php
mysql_connect('localhost','user','password');
mysql_select_db('cakephp_sample');
mysql_query('set names utf8');
$sql = sprintf(
"SELECT id,name,description FROM friends WHERE name LIKE '%s'",
mysql_real_escape_string('%'.$keyword.'%')
);
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
<?php
//index.php
$keyword = '';
}
$data[] = $row;
Model
if (isset($_POST['keyword'])) {
$keyword = $_POST['keyword'];
}
<!-- view.php -->
include ‘model.php’; <html>
include ‘view.php’; <head><title>テストページ</title></head>
Controller
<body>
<form action="list1.php" method="POST">
<input name="keyword"><input type="submit">
</form>
<ul>
<?php
foreach ($data as $row) {
echo '<li>';
echo $row['name'];
echo $row['description'];
echo '</li>';
}
View
?></ul>
</body></html>
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 19. 自然発生
進化の答えがMVC
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 20. CakePHPのMVC
Controller置き場
(全ての起点)
Model置き場
(データベース関連)
View置き場
(HTML関連)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 21. CakePHP習得の為に
Controllerの知識
Modelの知識
Viewの知識
それぞれの理解を深める
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 23. ポイント
pdo_mysql
mod_rewrite
PHP5.2以上
core.php / database.php
chmod -R 777 app/tmp
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 27. rewrite無しの場合
/**
* To configure CakePHP *not* to use mod_rewrite and to
* use CakePHP pretty URLs, remove these .htaccess
* files:
*
* /.htaccess
* /app/.htaccess
* /app/webroot/.htaccess
*
* And uncomment the App.baseUrl below:
*/
//Configure::write('App.baseUrl', env('SCRIPT_NAME'));
コメントを外す(rewriteが無理な時だけ)
urlの末尾にindex.phpを付ける
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 31. Controllerの必須テク
ディスパッチのルール
$scaffold $uses
$this->render()
$this->set()
$this->redirect()
$this->request
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 33. ページコントローラー
index.php
menu.php
mypage.php
に直接URLからアクセス
(ふつうのPHP)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 34. フロントコントローラー
index.php
にまずアクセスして、
その中でそれぞれの処理に
飛ばす
(CakePHPはこの方式)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 37. $scaffold
public $scaffold;
とするだけで標準機能が動く
※動作確認などに便利
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 38. $uses
class MyController extends AppController {
public $uses = array( Task , User );
}
コントローラーが使うモデルのリスト
省略時はコントローラー名と同一
命名規則で苦しむのは横着するからです
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 39. $this->render()
$this->render( hoge );
任意のViewを呼び出す
省略時はアクション名と同名を呼ぶ
同じViewを使いまわす事もできる!
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 40. $this->set()
$this->set( hoge , this is data );
任意のデータをViewへ渡す
スカラー値も配列も渡せる
Viewで使いたいデータはset()!
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 41. $this->redirect()
$this->redirect( oden/umai );
任意のページへリダイレクト
$this->flash() や
$this->Session->setFlash()
と組みわせてメッセージ表示
登録完了やエラー時によく使う
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 42. $this->request
$this->request->data
$this->request->pass
$this->request->is()
フォームの入力内容などにアクセス可
$_POSTなどを使った者は校庭10周に処す
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 45. Modelの必須テク
$useTable
$primaryKey
find()
save() saveField()
updateAll() delete()
deleteAll()
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 46. Modelの必須テク2
$validates
$belongsTo
$hasOne / $hasMany
$hasAndBelongsToMany
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 47. Model手強い
Modelを制する者は
Cakeを制す
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 48. $useTable
public $useTable = tablename ;
モデルが処理を行うテーブル名を指定
省略時はモデル名の複数形になる
falseにするとテーブル無しモデル
ここもはまりやすい
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 49. $primaryKey
public $primaryKey = id ;
テーブルの主キーを設定
省略時は id
既存のテーブルを使う場合に便利
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 50. find()
$this->find( all ,$options);
$this->find( first ,$options);
$this->find( count ,$options);
$this->find( list ,$options);
$this->find( threaded ,$options);
$this->find( neighbors ,$options);
オプション次第で自在にSQL文を実行
(オプションが多い)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 51. find()のオプション
array(
'conditions' => array('Model.field' => $thisValue),
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'),
'limit' => n,
'page' => n,
'offset' => n,
'callbacks' => true
)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 53. find()のオプション
array(
'conditions' => array('Model.field' => $thisValue),
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'),
'limit' => n,
'page' => n,
'offset' => n,
'callbacks' => true
)
必要ない部分は適宜省略可能
様々なfind()にこのオプションを使う
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 54. find( all ) の結果
$data = $this->Hoge->find( all );
$data[0][ Hoge ][ name ]; // Hogeのname
$data[0][ Hoge ][ twitter ]; // Hogeのtwitter
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 55. = 以外の比較
// field > a のような比較
array(
'conditions' => array('Model.field >' => $thisValue),
)
// field like %a% のような比較
array(
'conditions' => array('Model.field like' => % .$thisValue. % ),
)
キー名にスペースを空けて演算子
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 56. AND条件 OR条件
// AND
array(
'conditions' => array(
'Model.field <' => 100
'Model.field >' => 20
),
)
// OR
array(
'conditions' => array(
OR => array(
'Model.field' => 10,
'Model.type' => A ,
)
),
)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 57. find( first ) の結果
$data = $this->Hoge->find( first );
$data[ Hoge ][ name ]; // Hogeのname
$data[ Hoge ][ twitter ]; // Hogeのtwitter
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 58. find( count ) の結果
$data = $this->Hoge->find( count );
数値で帰ってくる
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 59. find( list ) の結果
$data = $this->Hoge->find( list );
リストで帰ってくる
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 60. find( threaded )
find( neighbors )
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 62. saveField()
Model->id = $id;
Model->saveField($name,$value);
単一のフィールドを更新
しかしあまり使わない
(save()で同じ事ができる)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 64. delete() deleteAll()
Model->delete($id,$cascade);
Model->deleteAll($condition,$cascade);
DELETEを実行できる。
しかし実際のシステムでDELETEは使わ
ず削除フラグなどで操作する事が多い。
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 65. $validates
public $validate = array(
'twitter' => array(
'rule' => 'notEmpty',
'message' => 'Twitter is very important'
),
'name' => array(
'rule' => 'notEmpty',
)
);
配列で設定したとおりに検査
save前に実行される
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 67. アソシエーション
$belongsTo
$hasOne / $hasMany
$hasAndBelongsToMany
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 68. $belongsTo
public $belongsTo = array(
'Shimokita' => array(
'foreignKey' => 'friendlist_id'
)
);
モデルが属する上位モデルを指定
日記(たくさん) ー> ユーザー(1)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 73. Viewの必須テク
Layoutの変更
$this->request
$this->element()
$this->Html
$this->Form
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 76. default.ctpの要所
<body>
<div id="container">
<div id="header">
<h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div>
<div id="footer">
<?php echo $this->Html->link($this->Html->image('cake.power.gif'),
'http://www.cakephp.org/',
array('target' => '_blank', 'escape' => false));?>
</div>
</div>
<?php echo $this->element('sql_dump'); ?>
</body>
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 77. CSSなど
<head>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo $cakeDescription ?>:
<?php echo $title_for_layout; ?>
</title>
<?php
echo $this->Html->meta('icon');
echo $this->Html->css('cake.generic');
echo $scripts_for_layout;
?>
</head>
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 81. $this->Html
$url = array(
controller => hoge ,
action => index
);
echo $this->Html->link($title,$url);
HTMLを生成するヘルパー
URLを自動生成して便利
(手動でタグは書かない)
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 84. CakePHPの応用技
MVCのバランス
MVCの各部品を強化
標準の強力な機能群
MVCの各要素に対応
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 85. ベストプラクティス
モデル層を厚くせよ
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 86. MVC::BHC
Behavior
Component
Model
Controller
Helper
View
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 87. 応用技に手を伸ばす時
コードが肥大化した時
まずはHelper
次にComponent
最後にBehavior
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 88. Helper
Html Paginator
Form Session
Cache Rss
Js
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 89. Component
Acl Pagination
Auth Security
Cookie Session
RequestHandler
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 90. Behavior
Tree
Containable
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 91. まだある応用技
プラグイン
Coreクラス
Console
ユニットテスト
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)
- 92. 今後の羅針盤
http://book.cakephp.org/2.0/en/
http://book.cakephp.org/2.0/ja/
3時間濃縮CakePHP2.1@札幌 / Yusuke Ando (@yando)