究極のコントローラを目指す

2,190
-1

Published on

第2回CakePHP関西勉強会

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,190
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
30
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide












































































































  • 究極のコントローラを目指す

    1. 1. 究極のControllerを目指す slywalker
    2. 2. 自己紹介原田 康生 Yasuo Harada大阪のこっそりPHPer忍び歩く男 - SLYWALKERhttp://d.hatena.ne.jp/slywalker/Twitter, Wassr, Hatena, GitHub IDslywalker
    3. 3. 自己紹介原田 康生 Yasuo Harada大阪のこっそりPHPer忍び歩く男 - SLYWALKERhttp://d.hatena.ne.jp/slywalker/Twitter, Wassr, Hatena, GitHub IDslywalker
    4. 4. Controllerのリファクタリングの お話です
    5. 5. お約束の画像
    6. 6. お約束の画像
    7. 7. お約束の画像Controllerをスリムに! Modelを厚く!
    8. 8. 具体的にどうすればいいの?
    9. 9. では、実際にやってみよう!
    10. 10. 前提条件記事投稿の機能の管理画面です各ユーザがログインして投稿します画像もあります
    11. 11. <?php � function edit($id = null) {class PostsController extends AppController { � � � � $conditions = array( � Post.id=>$id,� var $name = Posts; � � � Post.user_id=>$this->Auth->user(id), � � );� function index() { � � $post = $this->Post->find(first, compact(conditions));� � $this->paginate = array( � � if (!$post) {� � � conditions=>array( � � � $this->Session->setFlash(__(Invalid Post, true));� � � � Post.user_id=>$this->Auth->user(id), � � � $this->redirect(array(action=>index));� � � ), � � }� � ); � � if (!empty($this->data)) {� � $this->Post->recursive = 0; � � � if (!$this->data[Post][image][error]) {� � $this->set(posts, $this->paginate()); � � � � $filename = post.DS.$this->data[Post][image][name];� } � � � � move_uploaded_file( � � � � � $this->data[Post][image][tmp_name],� function view($id = null) { � � � � � WWW_ROOT.img.DS.$filename);� � $conditions = array( � � � � $this->data[Post][image] = $filename;� � � Post.id=>$id, � � � } else {� � � Post.user_id=>$this->Auth->user(id), � � � � unset($this->data[Post][image]);� � ); � � � }� � $post = $this->Post->find(first, compact(conditions)); � � � if ($this->Post->save($this->data)) {� � if (!$post) { � � � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->Session->setFlash(__(Invalid Post., true)); � � � � $this->redirect(array(action=>index));� � � $this->redirect(array(action=>index)); � � � } else {� � } � � � � $this->Session->setFlash(__(The Post could not be saved. Please, try� � $this->set(compact(post)); again., true));� } � � � } � � } else {� function add() { � � � $this->data = $post;� � if (!empty($this->data)) { � � }� � � if (!$this->data[Post][image][error]) { � }� � � � $filename = post.DS.$this->data[Post][image][name];� � � � move_uploaded_file( � function delete($id = null) {� � � � � $this->data[Post][image][tmp_name], � � $conditions = array(� � � � � WWW_ROOT.img.DS.$filename); � � � Post.id=>$id,� � � � $this->data[Post][image] = $filename; � � � Post.user_id=>$this->Auth->user(id),� � � } else { � � );� � � � unset($this->data[Post][image]); � � $post = $this->Post->find(first, compact(conditions));� � � } � � if (!$post) {� � � $this->data[Post][user_id] = $this->Auth->user(id); � � � $this->Session->setFlash(__(Invalid id for Post, true));� � � $this->Post->create(); � � � $this->redirect(array(action=>index));� � � if ($this->Post->save($this->data)) { � � }� � � � $this->Session->setFlash(__(The Post has been saved, � � if ($this->Post->del($id)) {true)); � � � $this->Session->setFlash(__(Post deleted, true));� � � � $this->redirect(array(action=>index)); � � � $this->redirect(array(action=>index));� � � } else { � � }� � � � $this->Session->setFlash(__(The Post could not be saved. � }Please, try again., true));� � � } }� � } ?>� }
    12. 12. <?php � function edit($id = null) {class PostsController extends AppController { � � � � $conditions = array( � Post.id=>$id,� var $name = Posts; � � � Post.user_id=>$this->Auth->user(id), � � );� function index() { � � $post = $this->Post->find(first, compact(conditions));� � $this->paginate = array( � � if (!$post) {� � � conditions=>array( � � � $this->Session->setFlash(__(Invalid Post, true));� � � � Post.user_id=>$this->Auth->user(id), � � � $this->redirect(array(action=>index));� � � ), � � }� � ); � � if (!empty($this->data)) {� � $this->Post->recursive = 0; � � � if (!$this->data[Post][image][error]) {� � $this->set(posts, $this->paginate()); � � � � $filename = post.DS.$this->data[Post][image][name];� } � � � � move_uploaded_file( � � � � � $this->data[Post][image][tmp_name],� function view($id = null) { � � � � � WWW_ROOT.img.DS.$filename);� � $conditions = array( � � � � $this->data[Post][image] = $filename;� � � Post.id=>$id, � � � } else {� � � Post.user_id=>$this->Auth->user(id), � � � � unset($this->data[Post][image]);� � ); � � � }� � $post = $this->Post->find(first, compact(conditions)); � � � if ($this->Post->save($this->data)) {� � if (!$post) { � � � � $this->Session->setFlash(__(The Post has been saved, true)); このControllerが� � � $this->Session->setFlash(__(Invalid Post., true)); � � � � $this->redirect(array(action=>index));� � � $this->redirect(array(action=>index)); � � � } else {� � } � � � � $this->Session->setFlash(__(The Post could not be saved. Please, try� � $this->set(compact(post)); again., true));� } � � � } � � } else {� function add() { � � � $this->data = $post;� � if (!empty($this->data)) { � � }� � � if (!$this->data[Post][image][error]) { � }� � � � $filename = post.DS.$this->data[Post][image][name];� � � � move_uploaded_file( � function delete($id = null) {� � � � � $this->data[Post][image][tmp_name], � � $conditions = array(� � � � � WWW_ROOT.img.DS.$filename); � � � Post.id=>$id,� � � � $this->data[Post][image] = $filename; � � � Post.user_id=>$this->Auth->user(id),� � � } else { � � );� � � � unset($this->data[Post][image]); � � $post = $this->Post->find(first, compact(conditions));� � � } � � if (!$post) {� � � $this->data[Post][user_id] = $this->Auth->user(id); � � � $this->Session->setFlash(__(Invalid id for Post, true));� � � $this->Post->create(); � � � $this->redirect(array(action=>index));� � � if ($this->Post->save($this->data)) { � � }� � � � $this->Session->setFlash(__(The Post has been saved, � � if ($this->Post->del($id)) {true)); � � � $this->Session->setFlash(__(Post deleted, true));� � � � $this->redirect(array(action=>index)); � � � $this->redirect(array(action=>index));� � � } else { � � }� � � � $this->Session->setFlash(__(The Post could not be saved. � }Please, try again., true));� � � } }� � } ?>� }
    13. 13. <?phpclass PostsController extends AppController {� var $name = Posts;}?>
    14. 14. こうなるまでの<?phpclass PostsController extends AppController {� var $name = Posts;}?>
    15. 15. こうなるまでの<?phpclass PostsController extends AppController {� var $name = Posts;}?> 実録公開
    16. 16. ※注意
    17. 17. ※注意• あくまでもリファクタリングの例です。
    18. 18. ※注意• あくまでもリファクタリングの例です。• 必ずしもここまでやれ! といったものではありません。
    19. 19. ※注意• あくまでもリファクタリングの例です。• 必ずしもここまでやれ! といったものではありません。• 用法、用量を守り 正しくお使いください。
    20. 20. <?php � function edit($id = null) {class PostsController extends AppController { � � � � $conditions = array( � Post.id=>$id,� var $name = Posts; � � � Post.user_id=>$this->Auth->user(id), � � );� function index() { � � $post = $this->Post->find(first, compact(conditions));� � $this->paginate = array( � � if (!$post) {� � � conditions=>array( � � � $this->Session->setFlash(__(Invalid Post, true));� � � � Post.user_id=>$this->Auth->user(id), � � � $this->redirect(array(action=>index));� � � ), � � }� � ); � � if (!empty($this->data)) {� � $this->Post->recursive = 0; � � � if (!$this->data[Post][image][error]) {� � $this->set(posts, $this->paginate()); � � � � $filename = post.DS.$this->data[Post][image][name];� } � � � � move_uploaded_file( � � � � � $this->data[Post][image][tmp_name],� function view($id = null) { � � � � � WWW_ROOT.img.DS.$filename);� � $conditions = array( � � � � $this->data[Post][image] = $filename;� � � Post.id=>$id, � � � } else {� � � Post.user_id=>$this->Auth->user(id), � � � � unset($this->data[Post][image]);� � ); � � � }� � $post = $this->Post->find(first, compact(conditions)); � � � if ($this->Post->save($this->data)) {� � if (!$post) { � � � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->Session->setFlash(__(Invalid Post., true)); � � � � $this->redirect(array(action=>index));� � � $this->redirect(array(action=>index)); � � � } else {� � } � � � � $this->Session->setFlash(__(The Post could not be saved. Please, try� � $this->set(compact(post)); again., true));� } � � � } � � } else {� function add() { � � � $this->data = $post;� � if (!empty($this->data)) { � � }� � � if (!$this->data[Post][image][error]) { � }� � � � $filename = post.DS.$this->data[Post][image][name];� � � � move_uploaded_file( � function delete($id = null) {� � � � � $this->data[Post][image][tmp_name], � � $conditions = array(� � � � � WWW_ROOT.img.DS.$filename); � � � Post.id=>$id,� � � � $this->data[Post][image] = $filename; � � � Post.user_id=>$this->Auth->user(id),� � � } else { � � );� � � � unset($this->data[Post][image]); � � $post = $this->Post->find(first, compact(conditions));� � � } � � if (!$post) {� � � $this->data[Post][user_id] = $this->Auth->user(id); � � � $this->Session->setFlash(__(Invalid id for Post, true));� � � $this->Post->create(); � � � $this->redirect(array(action=>index));� � � if ($this->Post->save($this->data)) { � � }� � � � $this->Session->setFlash(__(The Post has been saved, � � if ($this->Post->del($id)) {true)); � � � $this->Session->setFlash(__(Post deleted, true));� � � � $this->redirect(array(action=>index)); � � � $this->redirect(array(action=>index));� � � } else { � � }� � � � $this->Session->setFlash(__(The Post could not be saved. � }Please, try again., true));� � � } }� � } ?>� }
    21. 21. <?php � function edit($id = null) {class PostsController extends AppController { � � � � $conditions = array( � Post.id=>$id,� var $name = Posts; � � � Post.user_id=>$this->Auth->user(id), � � );� function index() { � � $post = $this->Post->find(first, compact(conditions));� � $this->paginate = array( � � if (!$post) {� � � conditions=>array( � � � $this->Session->setFlash(__(Invalid Post, true));� � � � Post.user_id=>$this->Auth->user(id), � � � $this->redirect(array(action=>index));� � � ), � � }� � ); � � if (!empty($this->data)) {� � $this->Post->recursive = 0; � � � if (!$this->data[Post][image][error]) {� � $this->set(posts, $this->paginate()); � � � � $filename = post.DS.$this->data[Post][image][name]; さて、� } � � � � move_uploaded_file( � � � � � $this->data[Post][image][tmp_name],� function view($id = null) { � � � � � WWW_ROOT.img.DS.$filename);� � $conditions = array( � � � � $this->data[Post][image] = $filename;� � � Post.id=>$id, � � � } else {� � � Post.user_id=>$this->Auth->user(id), � � � � unset($this->data[Post][image]);� � ); � � � }� � $post = $this->Post->find(first, compact(conditions)); � � � if ($this->Post->save($this->data)) {� � if (!$post) { � � � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->Session->setFlash(__(Invalid Post., true)); � � � � $this->redirect(array(action=>index)); Bad cake の中身を� � � $this->redirect(array(action=>index)); � � � } else {� � } � � � � $this->Session->setFlash(__(The Post could not be saved. Please, try� � $this->set(compact(post)); again., true));� } � � � } � � } else {� function add() { � � � $this->data = $post;� � if (!empty($this->data)) { � � }� � � if (!$this->data[Post][image][error]) { � }� � � � $filename = post.DS.$this->data[Post][image][name];� � � � move_uploaded_file( 見てみる � function delete($id = null) {� � � � � $this->data[Post][image][tmp_name], � � $conditions = array(� � � � � WWW_ROOT.img.DS.$filename); � � � Post.id=>$id,� � � � $this->data[Post][image] = $filename; � � � Post.user_id=>$this->Auth->user(id),� � � } else { � � );� � � � unset($this->data[Post][image]); � � $post = $this->Post->find(first, compact(conditions));� � � } � � if (!$post) {� � � $this->data[Post][user_id] = $this->Auth->user(id); � � � $this->Session->setFlash(__(Invalid id for Post, true));� � � $this->Post->create(); � � � $this->redirect(array(action=>index));� � � if ($this->Post->save($this->data)) { � � }� � � � $this->Session->setFlash(__(The Post has been saved, � � if ($this->Post->del($id)) {true)); � � � $this->Session->setFlash(__(Post deleted, true));� � � � $this->redirect(array(action=>index)); � � � $this->redirect(array(action=>index));� � � } else { � � }� � � � $this->Session->setFlash(__(The Post could not be saved. � }Please, try again., true));� � � } }� � } ?>� }
    22. 22. PostsController::index()� function index() {� � $this->paginate = array(� � � conditions=>array(� � � � Post.user_id=>$this->Auth->user(id),� � � ),� � );� � $this->Post->recursive = 0;� � $this->set(posts, $this->paginate());� }
    23. 23. PostsController::index()� function index() { user_idで絞り込み� � $this->paginate = array(� � � conditions=>array(� � � � Post.user_id=>$this->Auth->user(id),� � � ),� � );� � $this->Post->recursive = 0;� � $this->set(posts, $this->paginate());� }
    24. 24. PostsController::view()function view($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post., true));� � $this->redirect(array(action=>index));� }� $this->set(compact(post));}
    25. 25. PostsController::view() user_idで絞り込みfunction view($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post., true));� � $this->redirect(array(action=>index));� }� $this->set(compact(post));}
    26. 26. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id);
    27. 27. PostsController::add()function add() { FileUpload処理� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id);
    28. 28. PostsController::add()function add() { FileUpload処理� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id); user_idの付与
    29. 29. PostsController::add()� � $this->Post->create();� � if ($this->Post->save($this->data)) {� � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->redirect(array(action=>index));� � } else {� � � $this->Session->setFlash(__(The Post could not be saved.Please, try again., true));� � }� }}
    30. 30. PostsController::edit()function edit($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post, true));� � $this->redirect(array(action=>index));� }
    31. 31. PostsController::edit() user_idで絞り込みfunction edit($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post, true));� � $this->redirect(array(action=>index));� }
    32. 32. PostsController::edit()� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }
    33. 33. PostsController::edit() FileUpload処理� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }
    34. 34. PostsController::edit()� � if ($this->Post->save($this->data)) {� � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->redirect(array(action=>index));� � } else {� � � $this->Session->setFlash(__(The Post could not be saved.Please, try again., true));� � }� } else {� � $this->data = $post;� }}
    35. 35. PostsController::delete()function delete($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid id for Post, true));� � $this->redirect(array(action=>index));� }� if ($this->Post->del($id)) {� � $this->Session->setFlash(__(Post deleted, true));� � $this->redirect(array(action=>index));� }}
    36. 36. PostsController::delete()function delete($id = null) { user_idで絞り込み� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid id for Post, true));� � $this->redirect(array(action=>index));� }� if ($this->Post->del($id)) {� � $this->Session->setFlash(__(Post deleted, true));� � $this->redirect(array(action=>index));� }}
    37. 37. Model へ…
    38. 38. Modelに移すときの 私的原則各メソッドで共通の検索条件$this->data の繰作
    39. 39. 今回の場合は各メソッドで共通の検索条件• user_idの絞り込み$this->data の繰作• user_idの付与• FileUpload
    40. 40. user_idの絞り込みModel::beforeFind()を使うuser_idをModelにどうやって渡す?• Configure::write()• Configure::read()
    41. 41. PostsController::beforeFilter()function beforeFilter() {� parent::beforeFilter();� Configure::write(User.id, $this->Auth->user(id));}
    42. 42. PostsController::beforeFilter() user_idを書き込みfunction beforeFilter() {� parent::beforeFilter();� Configure::write(User.id, $this->Auth->user(id));}
    43. 43. Post::beforeFind()function beforeFind($queryData) {� $conditions = array(� � $this->alias..user_id=>Configure::read(User.id),� );� $queryData[conditions] = � Set::merge($queryData[conditions], $conditions);� return $queryData;}
    44. 44. Post::beforeFind() user_idを読み出しfunction beforeFind($queryData) {� $conditions = array(� � $this->alias..user_id=>Configure::read(User.id),� );� $queryData[conditions] = � Set::merge($queryData[conditions], $conditions);� return $queryData;}
    45. 45. Post::beforeFind() user_idを読み出しfunction beforeFind($queryData) {� $conditions = array(� � $this->alias..user_id=>Configure::read(User.id),� );� $queryData[conditions] = � Set::merge($queryData[conditions], $conditions);� return $queryData;} conditionsは多次元配列になりえる ので、Set::merge()を使う
    46. 46. PostsController::index()� function index() {� � $this->paginate = array(� � � conditions=>array(� � � � Post.user_id=>$this->Auth->user(id),� � � ),� � );� � $this->Post->recursive = 0;� � $this->set(posts, $this->paginate());� }
    47. 47. PostsController::index()� function index() {� � $this->paginate = array(� � � conditions=>array(� � � � Post.user_id=>$this->Auth->user(id),� � � ),� � );� � $this->Post->recursive = 0;� � $this->set(posts, $this->paginate());� }
    48. 48. PostsController::index()� function index() {� � $this->paginate = array(� � � conditions=>array(� � � � Post.user_id=>$this->Auth->user(id),� � � ),� � );� � $this->Post->recursive = 0;� � $this->set(posts, $this->paginate());� }
    49. 49. PostsController::view()function view($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post., true));� � $this->redirect(array(action=>index));� }� $this->set(compact(post));}
    50. 50. PostsController::view()function view($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post., true));� � $this->redirect(array(action=>index));� }� $this->set(compact(post));}
    51. 51. PostsController::view()function view($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post., true));� � $this->redirect(array(action=>index));� }� $this->set(compact(post));}
    52. 52. PostsController::edit()function edit($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post, true));� � $this->redirect(array(action=>index));� }
    53. 53. PostsController::edit()function edit($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post, true));� � $this->redirect(array(action=>index));� }
    54. 54. PostsController::edit()function edit($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid Post, true));� � $this->redirect(array(action=>index));� }
    55. 55. PostsController::delete()function delete($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid id for Post, true));� � $this->redirect(array(action=>index));� }� if ($this->Post->del($id)) {� � $this->Session->setFlash(__(Post deleted, true));� � $this->redirect(array(action=>index));� }}
    56. 56. PostsController::delete()function delete($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid id for Post, true));� � $this->redirect(array(action=>index));� }� if ($this->Post->del($id)) {� � $this->Session->setFlash(__(Post deleted, true));� � $this->redirect(array(action=>index));� }}
    57. 57. PostsController::delete()function delete($id = null) {� $conditions = array(� � Post.id=>$id,� � Post.user_id=>$this->Auth->user(id),� );� $post = $this->Post->find(first, compact(conditions));� if (!$post) {� � $this->Session->setFlash(__(Invalid id for Post, true));� � $this->redirect(array(action=>index));� }� if ($this->Post->del($id)) {� � $this->Session->setFlash(__(Post deleted, true));� � $this->redirect(array(action=>index));� }}
    58. 58. user_idの付与Model::beforeValidate()を使うConfigure::read()を使う
    59. 59. Post::beforeValidate()function beforeValidate($options = array()) {� $this->data[$this->alias][user_id] = Configure::read(User.id);� return true;}
    60. 60. Post::beforeValidate() user_idを読み出しfunction beforeValidate($options = array()) {� $this->data[$this->alias][user_id] = Configure::read(User.id);� return true;}
    61. 61. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id);
    62. 62. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id);
    63. 63. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� � $this->data[Post][user_id] = $this->Auth->user(id);
    64. 64. FileUploadModel::beforeSave()を使う
    65. 65. Post::beforeSave()function beforeSave($options = array()) {� if (!$this->data[$this->alias][image][error]) {� � $filename = � � low($this->alias).DS.$this->data[$this->alias][image][name];� � move_uploaded_file(� � � $this->data[$this->alias][image][tmp_name],� � � WWW_ROOT.img.DS.$filename);� � $this->data[$this->alias][image] = $filename;� } else {� � unset($this->data[$this->alias][image]);� }�� return true;}
    66. 66. Post::beforeSave() FileUpload処理function beforeSave($options = array()) {� if (!$this->data[$this->alias][image][error]) {� � $filename = � � low($this->alias).DS.$this->data[$this->alias][image][name];� � move_uploaded_file(� � � $this->data[$this->alias][image][tmp_name],� � � WWW_ROOT.img.DS.$filename);� � $this->data[$this->alias][image] = $filename;� } else {� � unset($this->data[$this->alias][image]);� }�� return true;}
    67. 67. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� �
    68. 68. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� �
    69. 69. PostsController::add()function add() {� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }� �
    70. 70. PostsController::edit()� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }
    71. 71. PostsController::edit()� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }
    72. 72. PostsController::edit()� if (!empty($this->data)) {� � if (!$this->data[Post][image][error]) {� � � $filename = post.DS.$this->data[Post][image][name];� � � move_uploaded_file(� � � � $this->data[Post][image][tmp_name],� � � � WWW_ROOT.img.DS.$filename);� � � $this->data[Post][image] = $filename;� � } else {� � � unset($this->data[Post][image]);� � }
    73. 73. <?php � function edit($id = null) {class PostsController extends AppController { � � � � $conditions = array( � Post.id=>$id,� var $name = Posts; � � � Post.user_id=>$this->Auth->user(id), � � );� function index() { � � $post = $this->Post->find(first, compact(conditions));� � $this->paginate = array( � � if (!$post) {� � � conditions=>array( � � � $this->Session->setFlash(__(Invalid Post, true));� � � � Post.user_id=>$this->Auth->user(id), � � � $this->redirect(array(action=>index));� � � ), � � }� � ); � � if (!empty($this->data)) {� � $this->Post->recursive = 0; � � � if (!$this->data[Post][image][error]) {� � $this->set(posts, $this->paginate()); � � � � $filename = post.DS.$this->data[Post][image][name];� } � � � � move_uploaded_file( � � � � � $this->data[Post][image][tmp_name],� function view($id = null) { � � � � � WWW_ROOT.img.DS.$filename);� � $conditions = array( � � � � $this->data[Post][image] = $filename;� � � Post.id=>$id, � � � } else {� � � Post.user_id=>$this->Auth->user(id), � � � � unset($this->data[Post][image]);� � ); � � � }� � $post = $this->Post->find(first, compact(conditions)); � � � if ($this->Post->save($this->data)) {� � if (!$post) { � � � � $this->Session->setFlash(__(The Post has been saved, true));� � � $this->Session->setFlash(__(Invalid Post., true)); � � � � $this->redirect(array(action=>index));� � � $this->redirect(array(action=>index)); � � � } else {� � } � � � � $this->Session->setFlash(__(The Post could not be saved. Please, try� � $this->set(compact(post)); again., true));� } � � � } � � } else {� function add() { � � � $this->data = $post;� � if (!empty($this->data)) { � � }� � � if (!$this->data[Post][image][error]) { � }� � � � $filename = post.DS.$this->data[Post][image][name];� � � � move_uploaded_file( � function delete($id = null) {� � � � � $this->data[Post][image][tmp_name], � � $conditions = array(� � � � � WWW_ROOT.img.DS.$filename); � � � Post.id=>$id,� � � � $this->data[Post][image] = $filename; � � � Post.user_id=>$this->Auth->user(id),� � � } else { � � );� � � � unset($this->data[Post][image]); � � $post = $this->Post->find(first, compact(conditions));� � � } � � if (!$post) {� � � $this->data[Post][user_id] = $this->Auth->user(id); � � � $this->Session->setFlash(__(Invalid id for Post, true));� � � $this->Post->create(); � � � $this->redirect(array(action=>index));� � � if ($this->Post->save($this->data)) { � � }� � � � $this->Session->setFlash(__(The Post has been saved, � � if ($this->Post->del($id)) {true)); � � � $this->Session->setFlash(__(Post deleted, true));� � � � $this->redirect(array(action=>index)); � � � $this->redirect(array(action=>index));� � � } else { � � }� � � � $this->Session->setFlash(__(The Post could not be saved. � }Please, try again., true));� � � } }� � } ?>� }
    74. 74. <?phpclass PostsController extends AppController { var $name = Posts; function beforeFilter() { parent::beforeFilter(); Configure::write(User.id, $this->Auth->user(id)); } function index() { $this->Post->recursive = 0; $this->set(posts, $this->paginate()); } function view($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post., true)); $this->redirect(array(action=>index)); } $this->set(compact(post)); } function add() { if (!empty($this->data)) { $this->Post->create(); if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } } function edit($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post, true)); $this->redirect(array(action=>index)); } if (!empty($this->data)) { if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } else { $this->data = $post; } } function delete($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid id for Post, true)); $this->redirect(array(action=>index)); } if ($this->Post->del($id)) { $this->Session->setFlash(__(Post deleted, true)); $this->redirect(array(action=>index)); } }}?>
    75. 75. <?php class PostsController extends AppController { var $name = Posts; function beforeFilter() { parent::beforeFilter(); Configure::write(User.id, $this->Auth->user(id)); } function index() { $this->Post->recursive = 0; $this->set(posts, $this->paginate()); } function view($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post., true)); $this->redirect(array(action=>index)); } $this->set(compact(post)); } function add() { if (!empty($this->data)) { $this->Post->create(); if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index));ふぅ…先は長い } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } } function edit($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post, true)); $this->redirect(array(action=>index)); } if (!empty($this->data)) { if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } else { $this->data = $post; } } function delete($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid id for Post, true)); $this->redirect(array(action=>index)); } if ($this->Post->del($id)) { $this->Session->setFlash(__(Post deleted, true)); $this->redirect(array(action=>index)); } } } ?>
    76. 76. Component へ…
    77. 77. Componentに移すときの 私的原則フックメソッドでの処理2つ以上のControllerで共通の処理
    78. 78. 今回の場合はフックメソッドでの処理• beforeFilter()2つ以上のControllerで共通の処理• なし
    79. 79. AccountComponent<?phpclass AccountComponent extends Object { var $components = array(Auth); function startup(&$controller) { Configure::write(User.id, $this->Auth->user(id)); }}?>
    80. 80. AccountComponent startup()にて設定<?phpclass AccountComponent extends Object { var $components = array(Auth); function startup(&$controller) { Configure::write(User.id, $this->Auth->user(id)); }}?>
    81. 81. PostsController::beforeFilter()function beforeFilter() { parent::beforeFilter(); Configure::write(User.id, $this->Auth->user(id));}
    82. 82. PostsController::beforeFilter()function beforeFilter() { parent::beforeFilter(); Configure::write(User.id, $this->Auth->user(id));}
    83. 83. PostsController<?phpclass PostsController extends AppController { var $name = Posts; var $components = array(Account);
    84. 84. PostsController<?phpclass PostsController extends AppController { var $name = Posts; var $components = array(Account);
    85. 85. <?phpclass PostsController extends AppController { var $name = Posts; function beforeFilter() { parent::beforeFilter(); Configure::write(User.id, $this->Auth->user(id)); } function index() { $this->Post->recursive = 0; $this->set(posts, $this->paginate()); } function view($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post., true)); $this->redirect(array(action=>index)); } $this->set(compact(post)); } function add() { if (!empty($this->data)) { $this->Post->create(); if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } } function edit($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post, true)); $this->redirect(array(action=>index)); } if (!empty($this->data)) { if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } else { $this->data = $post; } } function delete($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid id for Post, true)); $this->redirect(array(action=>index)); } if ($this->Post->del($id)) { $this->Session->setFlash(__(Post deleted, true)); $this->redirect(array(action=>index)); } }}?>
    86. 86. <?phpclass PostsController extends AppController { var $name = Posts; var $components = array(‘Account’); function index() { $this->Post->recursive = 0; $this->set(posts, $this->paginate()); } function view($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post., true)); $this->redirect(array(action=>index)); } $this->set(compact(post)); } function add() { if (!empty($this->data)) { $this->Post->create(); if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } まだまだ… } function edit($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid Post, true)); $this->redirect(array(action=>index)); } if (!empty($this->data)) { if ($this->Post->save($this->data)) { $this->Session->setFlash(__(The Post has been saved, true)); $this->redirect(array(action=>index)); } else { $this->Session->setFlash(__(The Post could not be saved. Please, try again., true)); } } else { $this->data = $post; } } function delete($id = null) { $conditions = array( Post.id=>$id, ); $post = $this->Post->find(first, compact(conditions)); if (!$post) { $this->Session->setFlash(__(Invalid id for Post, true)); $this->redirect(array(action=>index)); } if ($this->Post->del($id)) { $this->Session->setFlash(__(Post deleted, true)); $this->redirect(array(action=>index)); } }}?>
    87. 87. AppController へ…
    88. 88. AppControllerに移すときの 私的原則フックメソッドでの処理2つ以上のControllerで共通の処理
    89. 89. 今回の場合はフックメソッドでの処理• 既にComponentに移しているので、 なし2つ以上のControllerで共通の処理• 今回の例では、Controllerが一つしか ないが、同様の処理(例えばリンク集 とか)のControllerがあるとして
    90. 90. ポイント1PostController$this->Post->save()AppController$this->{$this->modelClass}->save()
    91. 91. ポイント2 PostController$this->set(‘posts’, $this->paginate()); AppController$name = Inflector::pluralize( Inflector::variable($this->modelClass));$this->set($name, $this->paginate());
    92. 92. class InflectorInflector ::実用的な各種の組み込みライブラリ ::マニュアル :: 1.2 Collection ::The Cookbook http://book.cakephp.org/ja/view/491/Inflector
    93. 93. class InflectorInflector クラスは文字列を扱い、複数形やキャメル記法といったように単語のバリエーションをハンドルする操作を行うもの - The Cookbook より
    94. 94. class Inflector - The Cookbook より
    95. 95. class Inflector複数Controllerでの共通の処理を書くとき、必要になるので覚えておく!
    96. 96. AppController::index()function index() {� $this->{$this->modelClass}->recursive = 0;� $name = Inflector::pluralize( Inflector::variable($this->modelClass));� $this->set($name, $this->paginate());}
    97. 97. AppController::view()function view($id = null) {� $conditions = array(� � $this->modelClass..id=>$id,� );� $data = $this->{$this->modelClass}->find(� � first, compact(conditions));� if (!$data) {� � $this->Session->setFlash(__(Invalid . $this->modelClass.., true));� � $this->redirect(array(action=>index));� }� $name = Inflector::singularize(� � Inflector::variable($this->params[controller]));� $this->set($name, $data);}
    98. 98. AppController::add()function add() {� if (!empty($this->data)) {� � $this->{$this->modelClass}->create();� � if ($this->{$this->modelClass}->save($this->data)) {� � � $this->Session->setFlash(__(The .$this->modelClass. has beensaved, true));� � � $this->redirect(array(action=>index));� � } else {� � � $this->Session->setFlash(__(The .$this->modelClass. could notbe saved. Please, try again., true));� � }� }}
    99. 99. AppController::edit()function edit($id = null) {� $conditions = array(� � $this->modelClass..id=>$id,� );� $data = $this->{$this->modelClass}->find(first, compact(conditions));� if (!$data) {� � $this->Session->setFlash(__(Invalid .$this->modelClass, true));� � $this->redirect(array(action=>index));� }� if (!empty($this->data)) {� � if ($this->{$this->modelClass}->save($this->data)) {� � � $this->Session->setFlash(__(The .$this->modelClass. has been saved, true));� � � $this->redirect(array(action=>index));� � } else {� � � $this->Session->setFlash(__(The .$this->modelClass. could not be saved. Please, tryagain., true));� � }� } else {� � $this->data = $post;� }}
    100. 100. AppController::delete()function delete($id = null) {� $conditions = array(� � $this->modelClass..id=>$id,� );� $data = $this->{$this->modelClass}->find(first, compact(conditions));� if (!$data) {� � $this->Session->setFlash(__(Invalid id for .$this->modelClass,true));� � $this->redirect(array(action=>index));� }� if ($this->{$this->modelClass}->del($id)) {� � $this->Session->setFlash(__($this->modelClass. deleted, true));� � $this->redirect(array(action=>index));� }}
    101. 101. Componentsを AppControllerへ PostControllerclass PostController extends AppController {� var $components = array(Account); AppControllerclass AppController extends Controller {� var $components = array(Auth, Account);

    ×