Intro a cakephp
Transcript of Intro a cakephp
![Page 1: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/1.jpg)
CakePHPßetaBeers Valencia, 01/12/2011
![Page 2: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/2.jpg)
Sobre mi
● Andy Dawson● @AD7six● Blogger infrequente AD7six.com● Desarrollador desde 1999● PHP desde 2005● CakePHP
○ desde 2005 ○ Autor de muchos plugins○ Miembro del equipo core de CakePHP
![Page 3: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/3.jpg)
Tecnologías y Herramientas
![Page 4: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/4.jpg)
Tecnologías y Herramientas
![Page 5: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/5.jpg)
¿Por que usar un framework?
● Estructura● Se concentra en implementar la aplicación● Evita problemas comunes● Consigue un código reutilizable● Se beneficia del conocomiento de otros
desarolladores● Desarrollo Rápido (RAD)
![Page 6: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/6.jpg)
Los problemas de los frameworks
● Estructura● Falta de flexibilidad● Complejidad● Tiempo para aprender● Rendimiento● Hay muchos - ¿cómo elejir?
![Page 7: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/7.jpg)
CakePHP en breve
● Convención sobre Configuración● Estructura MVC● Fácil de usar, rápido de aprender● RAD prototyping (scaffolding)● Generdor de código (bake)● unit test (phpunit)● Gran cantidad de plugins publicados● Gran comunidad● versión 1.3, php 4.3.2+● versión 2.0, php 5.2.9+ (y pdo)
![Page 8: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/8.jpg)
Como Empezar
● Principal: http://cakephp.org● Repo: http://github.com/cakephp/cakephp● Docs: http://book.cakephp.org/2.0/● El grupo google: cakephp-esp● Irc: Freenode #cakephp-es
![Page 9: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/9.jpg)
Bienvenido a CakePHP
![Page 10: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/10.jpg)
Ficheros y carpetas
● ROOT○ app
■ Controler■ Model■ View■ weboot
● css● js
○ lib■ Cake
○ plugins○ vendors
![Page 11: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/11.jpg)
Ficheros y carpetas
● ROOT○ app
■ Controler■ Model■ View■ weboot
● css● js
○ lib■ Cake
○ plugins○ vendors
La estructura es una "convención" - Se puede mover fácilmente
![Page 12: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/12.jpg)
Bake - un generador de todo [andy@work:~/www/apps/cakephp(2.0)]$ Console/cake bake ---------------------------------------------------------------Interactive Bake Shell---------------------------------------------------------------[D]atabase Configuration[M]odel[V]iew[C]ontroller[P]roject[F]ixture[T]est case[Q]uitWhat would you like to Bake? (D/M/V/C/P/F/T/Q) >
![Page 13: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/13.jpg)
Primeros pasos: Prototipo
Como hacer el blog-tutorial en 15 segundos 1. Cargar sql en BBDD2. Generar controlador3. Ver resultado
![Page 14: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/14.jpg)
1) SQL /* First, create our posts table: */CREATE TABLE posts ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL); /* Then insert some posts for testing: */INSERT INTO posts (title,body,created) VALUES ('The title', 'This is the post body.', NOW());INSERT INTO posts (title,body,created) VALUES ('A title once again', 'And the post body follows.', NOW());INSERT INTO posts (title,body,created) VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
![Page 15: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/15.jpg)
2) Generar Controllador [andy@work:~/www/apps/cakephp(2.0)]$ Console/cake bake Controller PostsWelcome to CakePHP v2.0.4 Console---------------------------------------------------------------App : cakephpPath: /home/andy/www/apps/cakephp/--------------------------------------------------------------- Baking controller class for Posts... Creating file /home/andy/www/apps/cakephp/Controller/PostsController.phpWrote `/home/andy/www/apps/cakephp/Controller/PostsController.php`Bake is detecting possible fixtures... Baking test case for Posts Controller ... Creating file /home/andy/www/apps/cakephp/Test/Case/Controller/PostsControllerTest.phpWrote `/home/andy/www/apps/cakephp/Test/Case/Controller/PostsControllerTest.php`
![Page 16: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/16.jpg)
3) Ver Resultados
![Page 17: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/17.jpg)
Un Controlador de Scaffolding <?php// Controller/PostsController.phpApp::uses('AppController', 'Controller');/** * Posts Controller * */class PostsController extends AppController { /** * Scaffold * * @var mixed */
public $scaffold; }
![Page 18: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/18.jpg)
Puntos de interés
● Definir BBDD -> generar código● Si no existe un modelo - Cake usará un
modelo genérico● Es todo dinámico - no hay código para
modificar● Útil mientras el diseño de la BBDD no esté
definido
![Page 19: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/19.jpg)
¿Cambios en la BBDD?
ALTER TABLE postsADD COLUMN published TINYINT(1) DEFAULT 0 AFTER body;
![Page 20: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/20.jpg)
Las vistas se adaptan
![Page 21: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/21.jpg)
Siguiente Fase: Bake MVC
Como hacer el blog-tutorial en 15 segundos. Otra vez. 1. Cargar sql en BBDD2. Generar MVC3. Ver resultados
![Page 22: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/22.jpg)
2) Generar MVC [andy@work:~/www/apps/cakephp(2.0)]$ Console/cake bake all Post -q Baking model class for Post...Baking controller class for Posts...Baking test case for Posts Controller ...Baking `index` view file...Baking `view` view file...Baking `add` view file...Baking `edit` view file... Bake All complete
![Page 23: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/23.jpg)
3) Ver Resultados
![Page 24: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/24.jpg)
Diferencias con Scaffolding
Ahora hay: Controller/PostsController.phpModel/Post.phpTest/Case/Controller/PostsControllerTest.phpTest/Fixture/PostFixture.phpView/Posts/add.ctpView/Posts/edit.ctpView/Posts/index.ctpView/Posts/view.ctp
![Page 25: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/25.jpg)
No es dinámico <?php// Controller/PostsController.phpApp::uses('AppController', 'Controller');/** * Posts Controller */class PostsController extends AppController { /** * index method */
public function index() {$this->Post->recursive = 0;$this->set('posts', $this->paginate());
} ...
![Page 26: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/26.jpg)
Contiene ejemplos de uso <?php // View/Posts/edit.ctp ?><div class="posts form"><?php echo $this->Form->create('Post');?> <fieldset> <legend><?php echo __('Edit Post'); ?></legend> <?php echo $this->Form->input('id'); echo $this->Form->input('title'); echo $this->Form->input('body'); echo $this->Form->input('published'); ?> </fieldset><?php echo $this->Form->end(__('Submit'));?></div>...
![Page 27: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/27.jpg)
Se puede hacer aun más sencillo
<?php // View/Posts/edit.ctp ?><div class="posts form"> <?php echo $this->Form->create(); echo $this->Form->inputs(); echo $this->Form->end(); ?></div>
![Page 28: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/28.jpg)
Puntos de interés
● Cuidado con bake en modo no-interactivo, sobrescribe ficheros sin confirmar
● Scaffolding y el código de bake son visualmente iguales
● Usa Scaffolding si es útil para ti - o salta directamente a código que puedas tocar.
● Pero usa bake - ahorra mucho tiempo
![Page 29: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/29.jpg)
Modelos - clases importantes
● "Fat Models, Skinny Controllers" es un dicho al que hay que hacer caso
● Es mejor llena los modelos con logica, no los controlladores
● Ejemplo - validación
![Page 30: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/30.jpg)
Sin validación - GIGO
![Page 31: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/31.jpg)
La razón - un modelo flaco
<?php// Model/Post.phpApp::uses('AppModel', 'Model');/** * Post Model * */class Post extends AppModel {}
![Page 32: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/32.jpg)
Añadir validación (con Bake) <?phpApp::uses('AppModel', 'Model');class Post extends AppModel { public $displayField = 'title'; public $validate = array( 'title' => array( 'notempty' => array( 'rule' => array('notempty'), 'message' => 'Escribe un titulo, por favor' ), 'minlength' => array( 'rule' => array('minlength', 10), 'message' => 'Un poquito mas...' ), ...
![Page 33: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/33.jpg)
Resultados con validación
![Page 34: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/34.jpg)
Puntos de interés
● Las vistas (el ayudante formulario) conocen automáticamente las reglas de validación
● Hay muchas reglas de validación en el core● Es fácil escribir tus proprias reglas de
validación● Las reglas se puede desactivar o modificar
![Page 35: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/35.jpg)
Resumen de bake
● Se usa para generar projectos, ficheros/clases o todo a la vez
● Interactivo o no● Dinámico (Scaffolding) o no● Las plantillas de cada clase se puede
modificar● Detecta relaciones en la BBDD● El código se adapta a las relaciones● No es obligotario usarlo
![Page 36: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/36.jpg)
Explorando en mas detalle
● Mensajes de error útiles
![Page 37: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/37.jpg)
Ejemplo mensaje de error
![Page 38: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/38.jpg)
Explorando en mas detalle
● Mensajes de error útiles● Debug Kit - un gran ayuda
![Page 39: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/39.jpg)
Debug Kit - installado
![Page 40: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/40.jpg)
Debug Kit - Session data
![Page 41: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/41.jpg)
Debug Kit - Query Log
![Page 42: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/42.jpg)
Debug Kit - Timer
26 (ms)
![Page 43: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/43.jpg)
Explorando en mas detalle
● Mensajes de error útiles● Debug Kit - un gran ayuda● Otras comandos de la consola
![Page 44: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/44.jpg)
La consola
![Page 45: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/45.jpg)
Explorando en mas detalle
● Mensajes de error útiles● Debug Kit - un gran ayuda● Otras comandos de la consola● Projectos útiles/ejemplos
![Page 46: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/46.jpg)
Proyectos interesantes
● http://croogo.org- Blog/CMS● http://my.candycane.jp - Issue tracker● http://cakepackages.com - Plugin index● Mark Story Asset Compress
![Page 47: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/47.jpg)
Explorando en más detalle
● Mensajes de error útiles● Debug Kit - un gran ayuda● Otras comandos de la consola● Projectos utiles/ejemplos● Para leer: CakePHP 1.3 Application
Cookbook - Mariano Iglesias 2011
![Page 48: Intro a cakephp](https://reader036.fdocuments.ec/reader036/viewer/2022081418/55930fb51a28abf2788b4570/html5/thumbnails/48.jpg)