baking master class

15

Click here to load reader

Upload: pierre-martin

Post on 14-May-2015

1.982 views

Category:

Business


2 download

DESCRIPTION

Slides from the talk given by Neil Crookes during the CakeFest #3 - July 2009Note: the original pdf and the code related to this talk can be found on cakephp.org (http://cakephp.org/downloads/CakeFest/CakeFest%203%20-%20Berlin%202009/Neil%20Crookes%20-%20Bake%20Master%20Class)

TRANSCRIPT

Page 1: Baking Master Class

Baking master class

who am i?Neil CrookesSouthampton, UKMEng Electronic Engineering 2001Married Helen 2004Daughter Charlotte born May 2007Kid #2 due September 2009Senior Programmer @ Five by Five (Digital media agency)

LAMP 8 yearsCakePHP 3 years

Page 2: Baking Master Class

Baking master class

the plan...Intro to BakeHow does it work?Customising view templatesExtending core shell and tasks

Start with the theory... SorryStep by step practical examplesAll code available... Shortly... Hopefully with history too

Demonstrate the awesome power of bakeEncourage you to write your own extensions... And share them with me ;-)

Page 3: Baking Master Class

Baking master class

intro to bakeConsole scriptCollection of a shell, several tasks and templatesCode generation<?php echo “<?php echo \$html->link(‘Master baker’,

‘http://neilcrookes.com’); ?>\n”; ?>Great for admin... But less likely you’ll use it for front end

http://book.cakephp.org/view/108/The-CakePHP-Consolehttp://book.cakephp.org/view/113/Code-Generation-with-Bakehttp://cakephp.org/screencasts/view/6

Page 4: Baking Master Class

Baking master class

how does it work?Bake shellTasks

ProjectDbConfigModel *Controller *View *PluginTest

TemplatesForm *HomeIndex *View

/cake/console/libs/bake.php/cake/console/libs/tasks

/cake/console/libs/templates/views

Page 5: Baking Master Class

Baking master class

the bake shellExtends ShellPrompts user for “thing” to bake, calls execute() on “thing” taskbake all

Bakes project if specifying a dir that does not exist in rootBakes database.php if does not exist in app/config

bake help

Page 6: Baking Master Class

Baking master class

ModelTask Also Extends Shell

execute() – Calls __interactive() or bake() depending on argslistAll() – Lists all available models based on db tablesgetName() – Prompts user to select one to bake__interactive() – Checks table name, primary key etc, confirmsselections back to userdoValidation() – * Prompts for a validation rule for each fielddoAssociations() – Prompts for associationsbake() – Constructs strings of PHP code & writes to model filebakeTest() – bakes the model test casefixture() – bakes the model test fixtures for the model test case

Page 7: Baking Master Class

Baking master class

ControllerTask Also Extends Shell

execute() – Calls __interactive() or bake() & bakeActions() dependingon argslistAll() – Lists all available controllersgetName() – Prompts user to select one to bake__interactive() – Prompts for scaffold/actions, admin actions,sessions, components, helpers etc, confirms selections back to userbakeActions() – Constructs string of PHP code for CRUD actionsbake() – Constructs strings of PHP code & writes to controller filebakeTest() – bakes the controller test case

Page 8: Baking Master Class

Baking master class

ViewTask Also Extends Shell

execute() – Calls __interactive() or bake() depending on args__interactive() – Prompts for scaffold actions, admin actions,confirms selections back to user__loadController() – Prepares variables to be available in the viewtemplates such as $pluralHumanName etc__associations() – Prepares associations variable to be available in theview templates, used for determining which actions are required etcgetContent() – uses output buffering around include() to get theprocessed contents of the templatebake() – Writes content to view file

Page 9: Baking Master Class

Baking master class

index view templateHTML with bit of PHP in the middleNormal PHP code gets interpreted by include() so if you want PHP code inyour views, you have to echo “<?php $code; ?>”;Uses vars from __loadController()Loops through fields in the model printing table headings and cellsIf field is a belongsTo foreignKey, makes it a link to view that recordPrints action links at the end of each table rowPrints pagination helper codeLoops through associations printing links to add and list associated modelrecords.

Page 10: Baking Master Class

Baking master class

form view templateShared by add & edit viewsLoops through fields in the model and HABTM associations printing$form->input()Prints link to index and delete record if edit viewLoops through associations printing links to add and list associated modelrecords.

Admin views use the same template as non-admin

Page 11: Baking Master Class

Baking master class

the practical bithttp://github.com/neilcrookes/CakeFest

Page 12: Baking Master Class

Baking master class

what to do to make it more awesome?Add markup & CSS hooks for your admin stylesheetIgnore certain fields in viewsMake views more DRYBake individual templatesBake templates for multiple controllersBake multiple controllersMake baked files use your own behaviors, components, helpers & elementsBake multiple validation rules per fieldEnhance usability, e.g. post save redirects, nicer error messages, formattimes

Page 13: Baking Master Class

Baking master class

what to do to make it even more awesome?Loads of UI widgets, e.g. File upload progress bars, date pickers, rich texteditors, drag and drop re-orderingLink if permission – integrate ACLFilter functionality on index viewsBreadcrumbs and browse on hierarchical model index viewsAjax validationAbstract controller logic to AppControllerModal add forms linked from select boxes for belongsTo foreignKey fields... And probably lots more

Page 14: Baking Master Class

Baking master class

the endThanks for listeningAny questions?

Page 15: Baking Master Class

Baking master class

get in [email protected]/neilcrookesgithub.com/neilcrookesMSN: [email protected]