Soumettre la recherche
Mettre en ligne
究極のコントローラを目指す
•
Télécharger en tant que KEY, PDF
•
4 j'aime
•
1,594 vues
Yasuo Harada
Suivre
第2回CakePHP関西勉強会
Lire moins
Lire la suite
Technologie
Affichage du diaporama
Signaler
Partager
Affichage du diaporama
Signaler
Partager
1 sur 125
Télécharger maintenant
Recommandé
Dependency Injection
Dependency Injection
Fabien Potencier
BEAR DI
BEAR DI
Akihito Koriyama
Drupal7 dbtng
Drupal7 dbtng
Nicolas Leroy
Building a Pluggable Plugin
Building a Pluggable Plugin
Brandon Dove
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
Six Apart KK
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
Drupal 8 database api
Drupal 8 database api
Viswanath Polaki
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
Nate Abele
Recommandé
Dependency Injection
Dependency Injection
Fabien Potencier
BEAR DI
BEAR DI
Akihito Koriyama
Drupal7 dbtng
Drupal7 dbtng
Nicolas Leroy
Building a Pluggable Plugin
Building a Pluggable Plugin
Brandon Dove
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
Six Apart KK
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
Drupal 8 database api
Drupal 8 database api
Viswanath Polaki
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
Nate Abele
The State of Lithium
The State of Lithium
Nate Abele
Database Design Patterns
Database Design Patterns
Hugo Hamon
Dependency Injection in Laravel
Dependency Injection in Laravel
HAO-WEN ZHANG
The Zen of Lithium
The Zen of Lithium
Nate Abele
The History of PHPersistence
The History of PHPersistence
Hugo Hamon
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Alessandro Nadalin
Command Bus To Awesome Town
Command Bus To Awesome Town
Ross Tuck
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
G Woo
J slider
J slider
Sesum Dragomir
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Ryan Mauger
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
Azim Kurt
Separation of concerns - DPC12
Separation of concerns - DPC12
Stephan Hochdörfer
Crazy things done on PHP
Crazy things done on PHP
Taras Kalapun
Spock and Geb
Spock and Geb
Christian Baranowski
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
Ross Tuck
BVJS
BVJS
Rebecca Murphey
Things I Believe Now That I'm Old
Things I Believe Now That I'm Old
Ross Tuck
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
ddiers
Command-Oriented Architecture
Command-Oriented Architecture
Luiz Messias
Quality Use Of Plugin
Quality Use Of Plugin
Yasuo Harada
Chapter 2 – Near East
Chapter 2 – Near East
Laura Moakley
Contenu connexe
Tendances
The State of Lithium
The State of Lithium
Nate Abele
Database Design Patterns
Database Design Patterns
Hugo Hamon
Dependency Injection in Laravel
Dependency Injection in Laravel
HAO-WEN ZHANG
The Zen of Lithium
The Zen of Lithium
Nate Abele
The History of PHPersistence
The History of PHPersistence
Hugo Hamon
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Alessandro Nadalin
Command Bus To Awesome Town
Command Bus To Awesome Town
Ross Tuck
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
G Woo
J slider
J slider
Sesum Dragomir
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Ryan Mauger
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
Azim Kurt
Separation of concerns - DPC12
Separation of concerns - DPC12
Stephan Hochdörfer
Crazy things done on PHP
Crazy things done on PHP
Taras Kalapun
Spock and Geb
Spock and Geb
Christian Baranowski
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
Ross Tuck
BVJS
BVJS
Rebecca Murphey
Things I Believe Now That I'm Old
Things I Believe Now That I'm Old
Ross Tuck
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
ddiers
Command-Oriented Architecture
Command-Oriented Architecture
Luiz Messias
Tendances
(20)
The State of Lithium
The State of Lithium
Database Design Patterns
Database Design Patterns
Dependency Injection in Laravel
Dependency Injection in Laravel
The Zen of Lithium
The Zen of Lithium
The History of PHPersistence
The History of PHPersistence
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Command Bus To Awesome Town
Command Bus To Awesome Town
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
J slider
J slider
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
Zend Framework and the Doctrine2 MongoDB ODM (ZF1)
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
Separation of concerns - DPC12
Separation of concerns - DPC12
Crazy things done on PHP
Crazy things done on PHP
Spock and Geb
Spock and Geb
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
BVJS
BVJS
Things I Believe Now That I'm Old
Things I Believe Now That I'm Old
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
Command-Oriented Architecture
Command-Oriented Architecture
En vedette
Quality Use Of Plugin
Quality Use Of Plugin
Yasuo Harada
Chapter 2 – Near East
Chapter 2 – Near East
Laura Moakley
Babylon system found in the web
Babylon system found in the web
Yasuo Harada
フレームワークの選び方・付き合い方
フレームワークの選び方・付き合い方
Yasuo Harada
Packagist
Packagist
Yasuo Harada
PHP Conference Kansai 2015
PHP Conference Kansai 2015
Yasuo Harada
En vedette
(6)
Quality Use Of Plugin
Quality Use Of Plugin
Chapter 2 – Near East
Chapter 2 – Near East
Babylon system found in the web
Babylon system found in the web
フレームワークの選び方・付き合い方
フレームワークの選び方・付き合い方
Packagist
Packagist
PHP Conference Kansai 2015
PHP Conference Kansai 2015
Similaire à 究極のコントローラを目指す
Presentation1
Presentation1
Rahadyan Gusti
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
Rafael Dohms
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
Yuya Takeyama
You code sucks, let's fix it
You code sucks, let's fix it
Rafael Dohms
The Art of Transduction
The Art of Transduction
David Stockton
Dependency Injection
Dependency Injection
Rifat Nabi
Bacbkone js
Bacbkone js
Артём Курапов
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
Masahiro Akita
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Sam Hennessy
Advanced php testing in action
Advanced php testing in action
Jace Ju
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
laravel tricks in 50minutes
laravel tricks in 50minutes
Barang CK
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Luis Curo Salvatierra
CodeIgniter Class Reference
CodeIgniter Class Reference
Jamshid Hashimi
Lithium Best
Lithium Best
Richard McIntyre
Oops in php
Oops in php
Gourishankar R Pujar
Coding for Scale and Sanity
Coding for Scale and Sanity
JimKellerES
画像Hacks
画像Hacks
Yusuke Wada
Min-Maxing Software Costs
Min-Maxing Software Costs
Konstantin Kudryashov
Intermediate PHP
Intermediate PHP
Bradley Holt
Similaire à 究極のコントローラを目指す
(20)
Presentation1
Presentation1
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
You code sucks, let's fix it
You code sucks, let's fix it
The Art of Transduction
The Art of Transduction
Dependency Injection
Dependency Injection
Bacbkone js
Bacbkone js
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Advanced php testing in action
Advanced php testing in action
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
laravel tricks in 50minutes
laravel tricks in 50minutes
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
CodeIgniter Class Reference
CodeIgniter Class Reference
Lithium Best
Lithium Best
Oops in php
Oops in php
Coding for Scale and Sanity
Coding for Scale and Sanity
画像Hacks
画像Hacks
Min-Maxing Software Costs
Min-Maxing Software Costs
Intermediate PHP
Intermediate PHP
Dernier
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
Antenna Manufacturer Coco
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Neo4j
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
apidays
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
Khem
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
naman860154
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
The Digital Insurer
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Drew Madelung
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
HampshireHUG
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Igalia
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
sudhanshuwaghmare1
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
Igalia
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Gabriella Davis
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
ThousandEyes
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Martijn de Jong
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
Pixlogix Infotech
Dernier
(20)
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
究極のコントローラを目指す
1.
究極の Controllerを目指す
slywalker
2.
自己紹介 原田 康生 Yasuo
Harada 大阪のこっそりPHPer 忍び歩く男 - SLYWALKER http://d.hatena.ne.jp/slywalker/ Twitter, Wassr, Hatena, GitHub ID slywalker
3.
自己紹介 原田 康生 Yasuo
Harada 大阪のこっそりPHPer 忍び歩く男 - SLYWALKER http://d.hatena.ne.jp/slywalker/ Twitter, Wassr, Hatena, GitHub ID slywalker
4.
Controllerの リファクタリングの
お話です
5.
お約束の画像
6.
お約束の画像
7.
お約束の画像 Controllerをスリムに!
Modelを厚く!
8.
具体的に どうすればいいの?
9.
では、 実際にやってみよう!
10.
前提条件 記事投稿の機能の管理画面です 各ユーザがログインして投稿します 画像もあります
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.
<?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.
<?php class PostsController extends
AppController { � var $name = 'Posts'; } ?>
14.
こうなるまでの <?php class PostsController extends
AppController { � var $name = 'Posts'; } ?>
15.
こうなるまでの <?php class PostsController extends
AppController { � var $name = 'Posts'; } ?> 実録公開
16.
※注意
17.
※注意 • あくまでもリファクタリングの例です。
18.
※注意 • あくまでもリファクタリングの例です。 • 必ずしもここまでやれ!
といったものではありません。
19.
※注意 • あくまでもリファクタリングの例です。 • 必ずしもここまでやれ!
といったものではありません。 • 用法、用量を守り 正しくお使いください。
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.
<?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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
38.
Model へ…
39.
Modelに移すときの
私的原則 各メソッドで共通の検索条件 $this->data の繰作
40.
今回の場合は 各メソッドで共通の検索条件 • user_idの絞り込み $this->data の繰作 •
user_idの付与 • FileUpload
41.
user_idの絞り込み Model::beforeFind()を使う user_idをModelにどうやって渡す? • Configure::write() • Configure::read()
42.
PostsController::beforeFilter() function beforeFilter() { �
parent::beforeFilter(); � Configure::write('User.id', $this->Auth->user('id')); }
43.
PostsController::beforeFilter()
user_idを書き込み function beforeFilter() { � parent::beforeFilter(); � Configure::write('User.id', $this->Auth->user('id')); }
44.
Post::beforeFind() function beforeFind($queryData) { �
$conditions = array( � � $this->alias.'.user_id'=>Configure::read('User.id'), � ); � $queryData['conditions'] = � Set::merge($queryData['conditions'], $conditions); � return $queryData; }
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; }
46.
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()を使う
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.
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.
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()); � }
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.
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.
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')); }
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.
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.
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')); � }
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.
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.
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')); � } }
59.
user_idの付与 Model::beforeValidate()を使う Configure::read()を使う
60.
Post::beforeValidate() function beforeValidate($options =
array()) { � $this->data[$this->alias]['user_id'] = Configure::read('User.id'); � return true; }
61.
Post::beforeValidate()
user_idを読み出し function beforeValidate($options = array()) { � $this->data[$this->alias]['user_id'] = Configure::read('User.id'); � return true; }
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.
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.
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');
65.
FileUpload Model::beforeSave()を使う
66.
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; }
67.
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; }
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.
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.
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']); � � } � �
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.
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.
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']); � � }
74.
<?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)); � � � } } � � } ?> � }
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.
<?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')); } } } ?>
77.
78.
Component へ…
79.
Componentに移すときの
私的原則 フックメソッドでの処理 2つ以上のControllerで共通の処理
80.
今回の場合は フックメソッドでの処理 • beforeFilter() 2つ以上のControllerで共通の処理 • なし
81.
AccountComponent <?php class AccountComponent extends
Object { var $components = array('Auth'); function startup(&$controller) { Configure::write('User.id', $this->Auth->user('id')); } } ?>
82.
AccountComponent
startup()にて設定 <?php class AccountComponent extends Object { var $components = array('Auth'); function startup(&$controller) { Configure::write('User.id', $this->Auth->user('id')); } } ?>
83.
PostsController::beforeFilter() function beforeFilter() {
parent::beforeFilter(); Configure::write('User.id', $this->Auth- >user('id')); }
84.
PostsController::beforeFilter() function beforeFilter() {
parent::beforeFilter(); Configure::write('User.id', $this->Auth- >user('id')); }
85.
PostsController <?php class PostsController extends
AppController { var $name = 'Posts'; var $components = array('Account');
86.
PostsController <?php class PostsController extends
AppController { var $name = 'Posts'; var $components = array('Account');
87.
<?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')); } } } ?>
88.
<?php class 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')); } } } ?>
89.
90.
AppController へ…
91.
AppControllerに移すときの
私的原則 フックメソッドでの処理 2つ以上のControllerで共通の処理
92.
今回の場合は フックメソッドでの処理 • 既にComponentに移しているので、 なし 2つ以上のControllerで共通の処理 •
今回の例では、Controllerが一つしか ないが、同様の処理(例えばリンク集 とか)のControllerがあるとして
93.
ポイント1 PostController $this->Post->save() AppController $this->{$this->modelClass}->save()
94.
ポイント2 PostController $this->set(‘posts’, $this->paginate());
AppController $name = Inflector::pluralize( Inflector::variable($this->modelClass)); $this->set($name, $this->paginate());
95.
class Inflector Inflector :: 実用的な各種の組み込みライブラリ
:: マニュアル :: 1.2 Collection :: The Cookbook http://book.cakephp.org/ja/view/491/ Inflector
96.
class Inflector Inflector クラスは文字列を扱い、 複数形やキャメル記法といったように 単語のバリエーションをハンドルする 操作を行うもの
- The Cookbook より
97.
class Inflector
- The Cookbook より
98.
class Inflector 複数Controllerでの共通の処理を書く とき、必要になるので覚えておく!
99.
AppController::index() function index() { �
$this->{$this->modelClass}->recursive = 0; � $name = Inflector::pluralize( Inflector::variable($this->modelClass)); � $this->set($name, $this->paginate()); }
100.
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); }
101.
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 been saved', true)); � � � $this->redirect(array('action'=>'index')); � � } else { � � � $this->Session->setFlash(__('The '.$this->modelClass.' could not be saved. Please, try again.', true)); � � } � } }
102.
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, try again.', true)); � � } � } else { � � $this->data = $post; � } }
103.
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')); � } }
104.
Componentsを
AppControllerへ PostController class PostController extends AppController { � var $components = array('Account'); AppController class AppController extends Controller { � var $components = array('Auth', 'Account');
105.
<?php class 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')); } } } ?>
106.
これぞ究極!? <?php class PostsController extends
AppController { � var $name = 'Posts'; } ?>
107.
おまけ Modelのフックメソッドへ移した処理 は、そのままBehavior化できる。
108.
UploadBehavior class UploadBehavior extends
ModelBehavior { function beforeSave(&$model, $options = array()) { if (!$model->data[$model->alias]['image']['error']) { $filename = low($model->alias).DS.$model- >data[$model->alias]['image']['name']; move_uploaded_file( $model->data[$model->alias]['image']['tmp_name'], WWW_ROOT.'img'.DS.$filename); $model->data[$model->alias]['image'] = $filename; } else { unset($model->data[$model->alias]['image']); } return true; } }
109.
Post class Post extends
AppModel { var $name = 'Post'; var $actsAs = array('Upload');
110.
Post class Post extends
AppModel { var $name = 'Post'; var $actsAs = array('Upload');
111.
参考になるもの http://thechaw.com/
112.
参考になるもの http://thechaw.com/cakebook
113.
参考になるもの http://thechaw.com/cakebook/source/ app_controller.php
114.
参考になるもの http://thechaw.com/cakebook/source/ app_controller.php
AppControllerに 共通処理をまとめてある
115.
他にも CakePHPを知り尽くしている人々の コードがたくさんあるので必見!!
116.
参考になるもの http://bakery.cakephp.org/
117.
参考になるもの http://bakery.cakephp.org/articles/view/ meioupload-behavior
118.
参考になるもの http://bakery.cakephp.org/articles/view/ meioupload-behavior これを見て、Upload処理は Behaviorがいいと思った
119.
英語だけれど… コードが読めればOK! 怖がらずに目を通してみよう!
120.
参考になるもの 翻訳記事 リファクタリングを考える時期の記事紹介 - cakephper の日記 http://d.hatena.ne.jp/cakephper/ 20090601/1243853035 元記事 5
signals that can indicate its time to re- factor ¦ Mark Story http://mark-story.com/posts/view/5-signals- that-can-indicate-its-time-to-re-factor
121.
リファクタリングを考える時期 メソッドのパラメータが多くなった時 ひとつのメソッドが多機能になってしまった時 同じようなコードがいくつも存在している時 名前の付け方が悪い時 あるべき場所(階層)にあるべきコードを書く
122.
多くはメソッドについて語られているが、 メソッドをクラスに置き換えても
同じ事がいえる
123.
リファクタリングを考える時期 クラスのメソッドが多くなった時 ひとつのクラスが多機能になってしまった時 同じようなコードがいくつも存在している時 名前の付け方が悪い時 あるべき場所(階層)にあるべきコードを書く
Notes de l'éditeur
Télécharger maintenant