Practical CakePHP Projects
Kai Chan and John Omokore with Richard K. Miller
Practical CakePHP Projects Copyright © 2...
32 downloads
1141 Views
3MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
Practical CakePHP Projects
Kai Chan and John Omokore with Richard K. Miller
Practical CakePHP Projects Copyright © 2009 by Kai Chan and John Omokore with Richard K. Miller All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1578-3 ISBN-13 (electronic): 978-1-4302-1579-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: David Golding Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editor: Marilyn Smith Associate Production Director: Kari Brooks-Copony Production Editor: Candace English Compositor: Patrick Cunningham Proofreader: Martha Whitt Indexer: Brenda Miller Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)juÀiÊÜ`iÞÊ>Û>>Li°ÊÃÌÊ«ÀiVÌÃÊ
>ÛiÊÌ}
ÌÊ`i>`iÃ]Ê>`ÊÞÕÊ want team members who can quickly pick up a new piece of technology.
Ê
UÊ >i**ÊÃÊi>ÃÞÊÌÊi>À°Ê9ÕÊÜ>ÌÊ>Ê«ÜiÀvÕÊÌÊÌ
>ÌÊÞÕÊV>Êi>ÃÞÊ>ÃÌiÀ°
Ê
UÊ >i**Ê
>ÃÊ}`ÊÃÕ««ÀÌ°Ê iÛi«iÀÃÊvÀiµÕiÌÞÊ«ÃÌÊ>`ÊÀi«ÞÊÌÊiÃÃ>}iÃÊÊÌ
iÊ Cake forum. And there are always some good discussions happening on the Cake IRC. (To see for yourself, simply download mIRC from dppl6++sss*ien_*_ki+, connect to the server en_*bnaajk`a*jap, and join the _]galdl channel.)
When you are developing a site using Cake, you often find yourself trawling through tutorials online to see how things are done. We’ve done that ourselves many times. However, despite the power of the Internet, we still like to look through books. And we think you will find this book a great help in your Cake development endeavors, in addition to all of the material available online. Most of the applications in this book have been written as a result of some real-world development we have done in the past. We focus on projects that we think are relevant to the future of web development. Let’s take mashups, for example. We should all take an interest in this ever-expanding area of web development. We can honestly say that any successful online web site in the future will need to easily communicate with other applications. Application designers will need to bear this in mind. Matters such as search engine optimization need to be built into the application itself. Cake allows us to think in terms of the high-level architecture instead of the nuts and bolts of a web application.
Who Should Read This Book Practical CakePHP is a book mainly for developers. To get the most from it, you should be comfortable with a number of web technologies and programming concepts. These include PHP, SQL, HTML, JavaScript, object-oriented programming, and design patterns, as well as the general principles of web development. If you are at the forefront of web development, then this book is for you!
xvii
xviii
NINTRO DUCT ION
If our book sounds a little too advanced for you, we recommend that you do some preliminary reading. We suggest the following books: Ê
UÊ Beginning PHP and MySQL: From Novice to Professional, Third Edition, by W. Jason Gilmore (Apress, 2008)
Ê
UÊ Beginning CakePHP: From Novice to Professional by David Golding (Apress, 2008)
How This Book Is Organized Each chapter in this book has been chosen so it will cover the core features in Cake, plus some of the minor features as well. The following is a rough breakdown of what each chapter includes. Ê
UÊ
>«ÌiÀÊ£]ʺ >iÊÕ`>iÌ>Ã]»Ê}ÛiÃÊÞÕÊ>ÊÌÀ`ÕVÌÊÌÊ >i°ÊvÊÞÕÊ>ÀiÊiÜÊÌÊ the CakePHP framework, this is the place to start.
Ê
UÊ
>«ÌiÀÊÓ]ʺ }}}]»Ê«ÀÛ`iÃÊÞÕÊÜÌ
Ê>ÊëiÊL}}}Ê>««V>Ì°Ê̽ÃÊ«iÀviVÌÊvÀÊ beginners who want to know what a Cake application looks like. If there are two chapters in the book that need to be read in sequence, they are Chapters 1 and 2.
Ê
UÊ
>«ÌiÀÊÎ]ʺ iÀVi]»Ê}ÛiÃÊÞÕÊ}Ài>ÌiÀÊÃ}
ÌÊÌÊÌ
iÊÜ>ÞÊ >iÊÃÊÕÃi`ÊÊ>Ê common application. We walk through implementing an online shop using the Cake framework.
Ê
UÊ
>«ÌiÀÊ{]ʺÊiÃÃ>}iÊÀÕÊ7iLÊ-iÀÛVi]»ÊVÛiÀÃÊÌ
iÊ`iÛi«iÌÊvÊ>ÊÜiLÊÃiÀÛViÃÊ API. We guide you through creating a clean API, so any third party can access your application using standard protocols.
Ê
UÊ
>«ÌiÀÊx]ʺ}iÊ>«ÃÊ>`ÊÌ
iÊ/À>Ûi}Ê->iÃ>]»ÊÃ
ÜÃÊÞÕÊ
ÜÊÌ
iÊ}iÊ Maps API is used with Cake. One of the main features of this chapter’s application relates to the classic traveling salesman problem: a salesman needs to visit a number of cities only once and return to where he started.
Ê
UÊ
>«ÌiÀÊÈ]ʺ>Ã
}Ê/ÜÌÌiÀÊÜÌ
ÊÌ
iÊ}iÊ/À>Ã>ÌÀ]»Êi«
>ÃâiÃÊÌ
iÊ«ÀÌ>ViÊ of web services in modern web application development. In true Web 2.0 and Cake fashion, this chapter’s application mashes the Google Ajax Language API with the Twitter API to provide automatic translation of Twitter messages.
Ê
UÊ
>«ÌiÀÊÇ]ʺ1ÌÊ/iÃÌ}Ê>`Ê7iLÊ/iÃÌ}]»ÊVÛiÀÃÊiÊvÊÌ
iÊ
ÌÌiÃÌÊÌ«VÃÊ>}ÊÜiLÊ professionals. Cake 1.2 devotes a large section to testing, and this chapter shows you how to take advantage of Cake’s integrated unit testing features.
Ê
UÊ
>«ÌiÀÊn]ÊºÊ >iÊ ÌÀÊ*>i]»ÊVÛiÀÃÊ >i½ÃÊ>VViÃÃÊVÌÀÊÃÌÃÊ>`ÊÃiVÕÀÌÞÊ features. We develop a web-based front end that allows administrators to manage user security.
NI N T R O D U C T I O N
Ê
UÊ
>«ÌiÀÊ]ʺ/À>Ã>Ì}Ê-ÌÀiÃ]»Ê«ÀÛ`iÃÊÞÕÊÜÌ
ÊÌ
iÊÜi`}iÊÌÊÌ>ViÊ >i½ÃÊ internationalization and localization features. We develop an application in which news stories are available in other languages, with an administration area where translators can translate stories from a base language to another language.
Ê
UÊ
>«ÌiÀÊ£ä]ʺ``}ÊÕÌ>}VÊi`Ã]»Ê`iÃÌÀ>ÌiÃÊiÝÌi`}Ê >i½ÃÊÕÃiÊvÊ>ÕÌ magic fields like _na]pa`, ik`ebea`, and pepha. We create three new automagic fields.
Ê
UÊ
>«ÌiÀÊ££]ʺ >iÊ/>}Ã]»ÊÃ
ÜÃÊÞÕÊÕÀÊÌ>iÊÊ>ÊiÃÌ>LÃ
i`ÊÌiV
}ÞÊÜ
iÀiÊ XML tags are used as a wrapper to coding logic. Using Cake, we develop our own HTML-based tags to display two Yahoo maps.
Ê
UÊ
>«ÌiÀÊ£Ó]ʺ Þ>VÊ >Ì>Êi`Ã]»ÊiÝÌi`ÃÊÌ
iÊiViÀViÊV
>«ÌiÀÊÜÌ
Ê>ÊëiV>Ê product-filtering technique. We take a dynamic data approach to product searches.
Ê
UÊ
>«ÌiÀÊ£Î]ʺ >«ÌV
>]»ÊÃ
ÜÃÊ
ÜÊ- ÊÀÌÊV>ÊLiÊÕÃi`Ê>ÃÊ>Ê >«ÌV
>ÊÌiÃÌ°ÊÊÌ
ÃÊ chapter’s project, the Captcha test is housed in a Cake component so it can be used by other applications.
How to Contact the Authors The authors can be contacted as follows: Ê
UÊ >Ê
>ÊV>ÊLiÊVÌ>VÌi`Ê>ÌÊg]e*_d]jÊVÀi>ÌiÊÞÕÀÊÜÊÊÕÃiÀÊ`ivi`ÊiÌ
`ÃÊ to manipulate data specific to the table a model object uses. ÊÃÌ}Ê£ÊÃ
ÜÃÊ>Ê>ÌiÀ>ÌÛiÊÜ>ÞÊvÊÀiÌÀiÛ}ÊvÀ>ÌÊ>LÕÌÊÌ
iÊ`i«>ÀÌiÌÃ* /
ÃÊÃÌ}ÊÜÊÀiÌÕÀÊiÝ>VÌÞÊÌ
iÊÃ>iÊÀiVÀ`ÃÊvÀÊÌ
iÊ`al]npiajpo table as the one shown ÊÊÃÌ}Ê£nÊÜ
iÊÕÃi`ÊÊÕÀÊ>}>ÀÞÊVÌÀiÀ°Ê/
iÊ`vviÀiViÊÃÊÌ
>ÌÊÊÃÌ}Ê£nÊÕÃiÃÊÌ
iÊ bej`$%ÊiÌ
`]ÊÜ
iÊÊÃÌ}Ê£ÊÕÃiÃÊÌ
iÊmqanu$% method to access data. Listing 1-9. Retrieving Records with the query() Method bqj_pekjcap@al]npiajp$ nacekj9jqhh%w omh9OAHA?P&BNKI\`al]npiajpo\SDANA\nacekj\9 nacekj7 napqnjpdeo):mqanu$ omh%7 y One advantage of using the mqanu$%ÊiÌ
`ÊÃÊÌ
>ÌÊÞÕÊV>Ê«ÕÌÊ>Ê>Ài>`ÞÊ`ivi`Ê-+Ê statement, from a legacy system, into this method without going through the trouble of dividing the query parameters into parts, as you would need to do to use the bej`$% method.
Data Validation >Ì>ÊÛ>`>ÌÊis an essential part of ensuring integrity and accuracy of data submitted by the ÕÃiÀ]ÊÃÕV
Ê>ÃÊÛ>Ê>ÊÜiLÊvÀ°Ê >iÊ
>ÃÊÊLÕÌÊÊÛ>`>ÌÊiV
>ÃðÊ9ÕÊëiVvÞÊÌ
iÊÛ>`>tion rules in a model, and Cake automatically applies the rules when a web form is connected ÌÊÌ
>ÌÊ`i°Ê/
iÃiÊÀÕiÃÊV>Ê>ÃÊLiÊ>««i`ÊÌÊ8Ê`>Ì>°
13
14
CH APT ER 1 N CAK E FU NDA MENTA L S
First, let’s add a simple validation rule to our @al]npiajp model using the r]he`]pa array, >ÃÊÃ
ÜÊÊÊÃÌ}Ê££ä°Ê/
iÊÛ>`>ÌÊÀÕiÊ>ÀÀ>ÞÊÃÊL>ÃV>ÞÊ>Ê>ÃÃV>ÌÛiÊ>ÀÀ>Þ°Ê/
iÊiÞÃÊ are the names of the form fields to validate, and the corresponding values represent the rules attached to the form fields. We’ll make use of this rule later, in the “Views” section. Listing 1-10. The Validation Rule for the Department Model r]n r]he`]pa9]nn]u$#nacekj#9:]nn]u$ #]hld]Jqiane_#9:]nn]u$ #nqha#9:#]hld]Jqiane_#( #namqena`#9:pnqa( #iaoo]ca#9:#Ajpan]nacekj*# % % %7 A field can have multiple validation rules. The r]he`]paÊ>ÀÀ>ÞÊÊÊÃÌ}Ê££äÊ`iviÃÊ a rule for the nacekj field in our @al]npiajp model. If a user does not submit a valid nacekj field, the model will return an error to the controller and quit committing the data to the `al]npiajpo database table. The iaoo]ca key deals with the error messages during validation. To display the error message on a form, use the form helper’s annkn function: 8;ldla_dkbkni):annkn$#nacekj#%7;: This will display the error message “Enter a region” if a user enters a nonalphanumeric value in the nacekj input field. «>ÀÌÊvÀÊÌ
iÊÀÕiÃÊi«Þi`ÊÊÊÃÌ}Ê££ä]Ê >iÊ«ÀÛ`iÃÊ>ÊÕLiÀÊvÊÊLÕÌÊÊ validation rules to check the validity of form inputs and ensure the integrity of information ÞÕÊÜ>ÌÊÌÊÃÌÀi°ÊÊ/>LiÊ£ÎÊÃÌÃÊ>ÊviÜÊvÊÌ
iÊÊLÕÌÊÊÀÕiÃÊ«ÀÛ`i`ÊLÞÊ >i° Table 1-3. Some of Cake’s Built-in Validation Rules
Rule
Description
Example
__
Checks for a valid credit card number
#nqha#9:]nn]u$#__#(#b]op#%
`]pa
Checks for a valid date
#nqha#9:#`]pa#
ai]eh
Checks for a valid e-mail address
#nqha#9:#ai]eh#
el
Checks for a valid IP address
#nqha#9:#el#
ldkja
Checks for a valid phone number
#nqha#9:]nn]u$#ldkja#(jqhh(#qg#%
For a complete list of the validation constants in your Cake build, see the predefined rules in the _]ga+he^o+r]he`]pekj*ldl file. 9ÕÊV>ÊiÝÌi`ÊÌ
iÊÃÌÊvÊÌ
iÊÀÕiÃÊLÞÊ>``}ÊÞÕÀÊÜÊÊÕÃiÀÊ`ivi`ÊÀÕiðÊÊÊÃÌ}Ê£££]Ê we define a simple custom rule to check if a value is a string.
C H A P T E R 1 N C A K E F U N D A M E N T A LS
Listing 1-11. A Custom Rule Called String bqj_pekjopnejc$ _da_g%w napqnjeo[opnejc$ _da_g%7 y Before you apply a custom rule, such as the opnejcÊÀÕiÊÃ
ÜÊÊÊÃÌ}Ê£££]Ê>``ÊÌÊÌÊÌ
iÊ _]ga+he^o+r]he`]pekj*ldl file, and then simply add the rule to your model r]he`]pa array: r]n r]he`]pa9]nn]u$#j]ia#9:#opnejc#%7 This will ensure that the j]ia field is a valid string. In upcoming chapters, you will come across more validation rules. The model object is robust and provides a lot of functionality for database manipulation. However, part of the data retrieved by a model is required for web surfers’ consumption. When >ÊÕÃiÀÊ>iÃÊ>Ê1,ÊÀiµÕiÃÌ]ÊÃiÊÀiëÃiÊÃÊiÝ«iVÌi`ÊÌÊLiÊ`ë>Þi`ÊÊ>ÊÛiÜ]ÊÜ
V
ÊÜi½Ê look at in the following section.
Views Now that ÜiÊ
>ÛiÊÃiÊÛ>`>ÌÊÀÕiÃ]Êi̽ÃÊLÕ`Ê>Ê/Êform to ask users to enter department information. The task of building a web form is done in a view. 6iÜÃÊ>ÀiÊ«ÀiÃiÌ>ÌÊ«>}iðÊ/
iÊ/ÊÀÊ8Ê`VÕiÌÃÊÊÌ
iÊ7iLÊ>ÀiÊÛiÜÃÊÌÊ the users. However, views can be anything, especially if Cake is used to output other formats iÊ,--]Ê* ]Ê>`ÊÃÊÊÜ
V
ÊÃÊViÀÌ>ÞÊ«ÃÃLiÊÜÌ
ÊÌ
iÊNamqaopD]j`han component and «>ÀÃ}ÊiÝÌiÃÃÊÊÌ
iÊÀÕÌiÀ®°Ê6iÜÃÊÀi`iÀÊvÀ>ÌÊÌÊÌ
iÊÕÃiÀðÊ6iÜÃÊ>ÀiÊV«Ãi`Ê vÊ>ÊÝÌÕÀiÊvÊ/Ê>`Ê**ÊV`i°Ê ÞÊ`iv>ÕÌ]Ê>ÊÛiÜÊÃ
Õ`ÊLiÊÃÌÀi`ÊÕ`iÀÊÌ
iÊVÌÀiÀÊ>iÊv`iÀ°ÊÀÊiÝ>«i]ÊÌ
iÊ view for the ]``$% method in the @al]npiajpo?kjpnkhhan is stored as ]ll+reaso+`al]npiajpo+ ]``*ldl. >Ì>ÊvÀÊÌ
iÊVÌÀiÀÊÃÊ«>ÃÃi`ÊÌÊÌ
iÊÛiÜÊLÞÊÕÃ}ÊÌ
iÊoap$% method in a controller.
NNote Views should be involved only with displaying output. For example, this is where you will see HTML tags and XML tags. Business logic, such as odkllejc[^]ogap[^]h]j_a9 jap[lne_a' p]t7, should not be in the view. However, the following is OK in a view: Eb$ odkllejc[^]ogap[^]h]j_a:-,,,%w a_dk#Ukq]naahece^habkn]`eo_kqjp#7y*
i̽ÃÊLÕ`ÊÌ
iÊÛiÜÊÌÊ>``ÊvÀ>ÌÊÌÊÕÀÊ`al]npiajpo database table. We are going to use another utility provided by Cake to build forms: the bkni object. ÊÃÌ}Ê££ÓÊÃ
ÜÃÊÌ
iÊ add view.
15
16
CH APT ER 1 N CAK E FU NDA MENTA L S
Listing 1-12. The Add View for the departments Table 8d-:=``@al]npiajp8+d-: 8;9 bkni):_na]pa$#`al]npiajp#(]nn]u$#]_pekj#9:#]``#%%7;: 8l:J]ia6 8;9 bkni):ejlqp$#@al]npiajp*j]ia#(]nn]u$#oeva#9:#-.,#%%7;: 8;9 bkni):annkn$#j]ia#%7;:8+l: 8l:Nacekj6 8;9 bkni):ejlqp$#@al]npiajp*nacekj#(]nn]u$#oeva#9:#0,#%%7;: 8;9 bkni):annkn$#nacekj#%7;:8+l: 8;9 bkni):aj`$#O]ra#%;: The view code created using the bkni object is stored in +]ll+reaso+`al]npiajp+]``*_pl. ,iiLiÀÊÌ
>ÌÊÌ
iÊÛ>`>ÌÊÀÕiÊvÀÊÌ
ÃÊvÀÊÃÊVÀi>Ìi`Ê>ÌÊÊÃÌ}Ê££ä° To display the add view to a user, we need a controller object with a function called ]``$%, Ü
V
ÊÌÀiÃÊÌÊ`ÊiÝ>VÌÞÊÜ
>ÌÊÌÊÃ>ÞÃ\Ê>``ÊÌ
iÊvÀÊ`>Ì>ÊÌÊÕÀÊ`al]npiajpo database table. ÊÃÌ}Ê££ÎÊshows the action ]``$% method of the @al]npiajpo?kjpnkhhan. Listing 1-13. The add() Action in the Departments Controller 8;ldl _h]oo@al]npiajpo?kjpnkhhanatpaj`o=ll?kjpnkhhanw bqj_pekj]``$%w eb$ailpu$ pdeo):`]p]%%w pdeo):@al]npiajp):_na]pa$%7 eb$ pdeo):@al]npiajp):o]ra$ pdeo):`]p]%%w pdeo):Oaooekj):oapBh]od$£ [[$#Pda@al]npiajp`]p]d]o^aajo]ra`#(pnqa%%7 pdeo):na`ena_p$]nn]u$#]_pekj#9:#]``#%%7 yahoaw pdeo):Oaooekj):oapBh]od$£ [[$#Pda@al]npiajp`]p]_kqh`jkp^ao]ra`*Lha]oa(pnu]c]ej*#(pnqa%%7 y y y ;: Now that we’ve built a web form using the /Ê
i«iÀÊ>`ÊVÀi>Ìi`Ê>ÊVÌÀiÀÊÌÊ handle the ]``$% action, let’s demonstrate how data is passed to a view. We’ll start by creating a show action in our @al]npiajpo controller class. The action method is shown in Ê ÃÌ}Ê££{°
C H A P T E R 1 N C A K E F U N D A M E N T A LS
Listing 1-14. The show() action in the Departments Controller bqj_pekjodks$ nacekj%w pdeo):oap$#`]p]#(pdeo):@al]npiajp):cap@al]npiajp$ nacekj%%7 y ÊÊÃÌ}Ê££{]ÊÌ
iÊodks$% method accepts the nacekj data as a parameter, and then retrieves departmental data and uses the oap$% method to prepare the data for the view in ÊÃÌ}Ê££x°ÊÊÛiÜÊÃÊ>Ü>ÞÃÊ>i`Ê>vÌiÀÊ>Ê>VÌ°ÊÀÊiÝ>«i]ÊÌ
iÊodks$% action in the @al]npiajpo?kjpnkhhanÊvÊÊÃÌ}Ê££{ÊÜÊ
>ÛiÊ>ÊÛiÜÊviÊÃÌÀi`ÊÊ]ll+reaso+`al]npiajp+ odks*_pl. Listing 1-15. A View for the Action show($region) of DepartmentsController 8d-_h]oo9benop: Kqn@al]npiajp6) 8;ldl bkna]_d$ `]p]]o `al]npiajp%w a_dk `al]npiajpW#@al]npiajp#YW#j]ia#Y7 y ;: 8+d-: 8l:Kqn@al]npiajpo]naha`^u@n*?]ga*8+l: /
iÊÛiÜÊÊÊÃÌ}Ê££xÊÜÊ`ë>ÞÊÌ
iÊ>iÊvÊÌ
iÊ`i«>ÀÌiÌÊvÀÊiÝ>«i]Ê->iîÊÊ the header section of the web page presented to the user.
NTip If you have a number of data items to display in a view, such as
pepha9Ln]_pe_]h?]ga Lnkfa_po7 `al]npiajp9O]hao7]j` nacekj9QG7, using pdeo):oap$_kil]_p$#pepha #(#`al]npiajp#(#nacekj#%%7 will enable you to use only one oap$% function in your controller class to
pass all the information to your view. You can then access the individual variable in your view; for example, 8;ldlln$ pepha%7;:.
An essential part of any framework is the part that handles requests. In the MVC structure, this is handled by the controller.
17
18
CH APT ER 1 N CAK E FU NDA MENTA L S
Controllers As you’ve seen, a controller is a class with methods called actions. These actions or methods VÌ>ÊÃÌÊvÊÌ
iÊ}VÊÌ
>ÌÊÀië`ÃÊÌÊÕÃiÀÊÀiµÕiÃÌÃÊÊ>Ê>««V>Ì°ÊÀÊiÝ>«i]ÊvÊ a user wants to know the number of departments in a particular region, the user needs to access the odks$% method of @al]npiajpo?kjpnkhhanÊ`ivi`ÊÊÊÃÌ}Ê££ä]ÊLÞÊÌÞ«}ÊÌ
iÊ vÜ}Ê1,ÊÊ>ÊLÀÜÃiÀÊ>``ÀiÃÃÊL>À\ hk_]hdkop+`al]npiajpo+odks+QO /
iÊ`iv>ÕÌÊÃÌÀÕVÌÕÀiÊvÀÊ>VViÃÃ}Ê>Ê >iÊ1,ÊÃÊÌÊvÀÃÌÊëiVvÞÊÌ
iÊVÌÀiÀÊ>`ÊÌ
iÊ Ì
iÊ>VÌ°ÊÊÌ
iÊ«ÀiVi`}Ê1,]Ê`al]npiajpo is the controller, odks is the action, and QO is a parameter. By convention, a Cake request should be structured in the following manner: dppl6++Wiu`ki]ej*_kiY+W=llhe_]pekjY+W?kjpnkhhanY+W=_pekjY+WL]n]i-Y+ÅWL]n]iJY
NNote The ej`at$%method is the default access point to a controller when a method is not explicitly specified in a user’s request. For example, you can load the ej`at$% method with codes that will invoke the welcome page of your application. However, do not forget to create a view, or you will get a warning from Cake stating that you should create a view for the action.
9ÕÀÊ>««V>̽ÃÊVÌÀiÀÊV>ÃÃiÃÊ>ÀiÊiÝ«iVÌi`ÊÌÊiÝÌi`ÊÌ
iÊ=ll?kjpnkhhan class, Ü
V
ÊÊÌÕÀÊiÝÌi`ÃÊ>ÊVÀiÊ?kjpnkhhan class, which is a standard Cake library. The =ll?kjpnkhhan class is defined in +]ll+]ll[_kjpnkhhan*ldl, and it should contain methods that are shared between two or more controllers. These controllers can include any number of actions. The =ll?kjpnkhhan serves as a global class that can contain properties and methods common to all the user-defined conÌÀiÀÃÊÊ>Ê>««V>Ì°ÊÀÊiÝ>«i]ÊÞÕÊV>Ê
>ÛiÊ>ÊiÌ
`ÊÌÊ`iÌiVÌÊ>`ÊiÝÌÀ>VÌÊÌ
iÊ*Ê address of a user, and then use the value of this address to determine the flow of the applicaÌ°Ê >ÀiÀ]ÊÊÊÃÌ}Ê£Ó]ÊÜiÊÕÃi`ÊÌ
iÊ`iv>ÕÌÊVÌÀiÀÊiÌ
`ÊV>i`Ê^abknaBehpan$% in our controller class to reference the method defined in the =ll?kjpnkhhan class stored in ]ll+ ]ll[_kjpnkhhan*ldl°ÊÌ
iÀÊëiÊiÝ>«iÊÃÊÌÊÃiÌÊ>Êdefault page title for an application, >ÃÊÊÌ
ÃÊiÝ>«i\ 8;ldl _h]oo=ll?kjpnkhhanatpaj`o?kjpnkhhanw r]n l]caPepha9#?d]lpan-Ì=>]ganu=llhe_]pekj#7 -ViÊÕÀÊÊÕÃiÀÊ`ivi`ÊVÌÀiÀÊiÝÌi`ÃÊÌ
iÊ=ll?kjpnkhhan, using the statement pdeo):L]caPepha within our controller gives us access to the string #?d]lpan-Ì=>]ganu =llhe_]pekj# assigned to the l]caPepha property in the =ll?kjpnkhhan class.
C H A P T E R 1 N C A K E F U N D A M E N T A LS
The qoao property is an important property within the controller. It works similarly to the namqena[kj_aÊÃÌ>ÌiiÌÊÊ**°Ê >ÃV>Þ]ÊViÊÞÕÊ
>ÛiÊVÀi>Ìi`Ê>Ê`iÊÃÕV
Ê>ÃÊ@al]npiajp®Ê and you want to use the model in a controller, you need to include it in the qoao array. For iÝ>«i]ÊÜ
iÀiÊ@al]npiajp and Pn]`aÊ>ÀiÊiÝÃÌ}Ê`iÃ]ÊÞÕÊV>Ê
>ÛiÊÌ
iÊvÜ}ÊÃÌ>Ìiment in your controller: qoao9]nn]u$#@al]npiajp#(#Pn]`a#%7*
Cake Components Components are classes defined to carry out specific application tasks to support the controller. Cake comes with many built-in components, such as =_hÊvÀÊÕÃiÀÊ>`Ê}ÀÕ«Ê>VViÃÃÊ VÌÀ®]Ê=qpdÊvÀÊÕÃiÀÊ>`Ê}ÀÕ«Ê>ÕÌ
iÌV>Ì®]ÊAi]eh, Oaooekj, and NamqaopD]j`han. Components can also be user-defined. In fact, in large web applications, you will most likely need to build some of your own components to be used by several controllers. All the components that you develop should be stored in the folder ]ll+_kjpnkhhano+_kilkjajpo. Components follow the same Cake conventions as controllers. ÀÊ>Ê«À}À>iÀ½ÃÊ«ÌÊvÊÛiÜ]ÊV«iÌÃÊi>LiÊÞÕÊÌÊiÝÌi`ÊÌ
iÊvÕVÌ>ÌÞÊ of Cake. If you find that your component is quite useful and you possess the free open source spirit, you can and should post it on the Cake web site, where there is a public repository of components. To demonstrate, we’ll dive straight in and create our own simple component—a utility to convert an array to an object. ÊÃÌ}Ê££ÈÊÃ
ÜÃÊÌ
iÊV`iÊÌÊVÀi>ÌiÊÌ
ÃÊV«iÌ° Listing 1-16. A Component to Convert an Array to an Object 8;ldl _h]oo=nn]uPkK^fa_p?kilkjajpatpaj`oK^fa_pw bqj_pekjop]npql$" _kjpnkhhan%w pdeo):?kjpnkhhan9 _kjpnkhhan7 y bqj_pekj_kjranp$ ]nn]u(" k^f%w bkna]_d$ ]nn]u]o gau9: r]hqa%w eb$eo[]nn]u$ r]hqa%%w k^f): gau9jasop`?h]oo$%7 pdeo):]nn]u[pk[k^f$ r]hqa(k^f): gau%7 yahoaw k^f): gau9 r]hqa7 y y napqnj k^f7 y y ;:
19
20
CH APT ER 1 N CAK E FU NDA MENTA L S
The =nn]uPkK^fa_p?kilkjajpÊV>ÃÃÊÊÊÃÌ}Ê££ÈÊVÌ>ÃÊÌÜÊL>ÃVÊvÕVÌÃ]ÊÜ
V
Ê>ÀiÊ stored in ]ll+_kjpnkhhano+_kilkjajpo+]nn]u[pk[k^fa_p*ldl: Ê
UÊ /
iÊvÀÃÌÊvÕVÌ]ÊV>i`Êop]npql$%, is used to instantiate the controller object. This enables all other functions within the component to access information contained in the parent controller. It’s basically a callback method used to bring the controller object into the component.
Ê
UÊ /
iÊÃiV`ÊvÕVÌ]Ê_kjranp$%, is our user-defined function. It does the work of >VVi«Ì}Ê>Ê>ÀÀ>ÞÊvÊ`>Ì>Ê>`ÊÀiÌÕÀ}ÊÌ
iÊ>ÀÀ>ÞÊ>ÃÊ>ÊLiVÌ°Ê9ÕÊV>ÊÕÃiÊÌ
ÃÊVponent whenever you want to convert an array to an object.
ÛiÀÞÌ
}ÊÃ`iÊ>ÊV«iÌÊÃ
Õ`ÊLiÊ}iiÀV°Ê ÊÌÊ«ÕÌÊÊVÌÀiÀÊëiVvVÊV`i]Ê such as a database table name, into components. 9ÕÊV>ÊÕÃiÊV«iÌÃÊÜÌ
Êcontrollers or other components. To use a component— whether it is a built-in one or one you have created—you need to first declare the component within the _kilkjajpo array in a user-defined controller, another component, or in the =ll?kjpnkhhanÊV>ÃðÊÀÊiÝ>«i]ÊÌÊÕÃiÊÌ
iÊV«iÌÊÊÊÃÌ}Ê££È]ÊVÕ`iÊÌ
iÊvÜ}Ê statement: r]n _kilkjajpo9]nn]u$#=nn]uPkK^fa_p#% ÊÊÃÌ}Ê££Ç]ÊÜiÊ>iÊÀiviÀiViÃÊÌÊÌ
iÊÊLÕÌÊÊOaooekj component and our =nn]uPkK^fa_p component in the @al]npiajpo?kjpnkhhan class. Listing 1-17. Using Components in DepartmentsController 8;ldl _h]oo@al]npiajpo?kjpnkhhanatpaj`o=ll?kjpnkhhan w r]n qoao9]nn]u$#@al]npiajp#%7 r]n _kilkjajpo9]nn]u$#Oaooekj#(#=nn]uPkK^fa_p#%7 bqj_pekj`eolh]u$%w ]nn@]p]9]nn]u$%7 ]nn@]p]9pdeo):bej`$#]hh#%7 ln$ pdeo):=nn]uPkK^fa_p):_kjranp$]nn@]p](" k^f%%7 y y ;: ÊÊÃÌ}Ê££Ç]ÊÜiÊVÛiÀÌÊÌ
iÊÀiÃÕÌÊvÊÌ
iÊ`>Ì>ÊÀiÌÀiÛi`ÊvÀÊÕÀÊ`al]npiajpo database table from an array to an object. First, we use the qoao array to reference the @al]npiajp model. We then use the _kilkjajp array to reference the Oaooekj component, which is a built-in Cake component, and the =nn]uPkK^fa_p component, which is our user-defined V«iÌ°Ê iÝÌ]ÊÜiÊVÀi>ÌiÊ>Ê`eolh]u$% function that contains a declaration of an array variable called ]nn@]p]. We retrieve the department data using the default bej`$% function, store the result in the array, and then pass the array to the _kjranp$% method of the =nn]uPkK^fa_p component. Finally, we use the Cake ln$% global function to print the resulting object.
C H A P T E R 1 N C A K E F U N D A M E N T A LS
Helpers Cake helpers are classes that help to decrease development time by providing shortcuts to generate presentational elements. Earlier, we used the Cake form helper, which helps with form element creation and data handling. Helper files should be stored in the ]ll+reaso+dahlano v`iÀ°ÊÊ/>LiÊ£{ÊLÀivÞÊ`iÃVÀLiÃÊÃiÊvÊ >i½ÃÊÊLÕÌÊÊ
i«iÀðÊÀÊvÕÊdocumentation of the Cake helpers, check the Cake API at dppl6++]le*_]galdl*knc. Table 1-4. Some of Cake’s Built-in Helpers
Helper
Description
/
Helps >ÕÌ>ÌiÊVÀi>Ì}Ê/ÊiiiÌÃÊ>`Ê>ÃÊi>LiÃÊ`Þ>VÊ}iiÀ>ÌÊvÊ /ÊÌ>}ÃÊLÞÊ>VVi«Ì}Ê>`Ê«>ÀÃ}ÊÛ>À>LiðÊ/
ÃÊ
i«iÀÊÃÊV>i`ÊÊ>ÊÛiÜÊLÞÊÕÃ}Ê the dpihÊLiVÌ°Ê/ÊVÕ`iÊ>ÊÀiviÀiViÊÌÊ/Ê
i«iÀ]ÊÕÃiÊÌ
iÊÛ>À>LiÊ dahlano 9]nn]u$#Dpih#%7°Ê/Ê
i«iÀÊvÕVÌÃÊÕÌ«ÕÌÊ/ÊiiiÌÃÊÃÕV
Ê>ÃÊ_d]noap, _oo, `er, `k_Pula, ei]ca, hejg, iap], jaopa`Heop, l]n], opuha, p]^haDa]`ano, p]^ha?ahho, and so on.
Form
Helps ÊvÀÊVÀi>ÌÊ>`Ê«ÀViÃÃ}°Ê1ÃiÊÌ
iÊ bkni object together with its funcÌÃÊÌÊVÀi>ÌiÊvÀÊiiiÌðÊÀÊiÝ>«i]ÊÌÊVÀi>ÌiÊ>ÊvÀÊ«ÕÌÊiiiÌ]ÊÕÃiÊÌ
iÊ bkni):ejlqp$% function. To start the form tag, use the bkni):_na]pa$% function. Other form input element functions include h]^ah, _da_g^kt, `]paPeia, de``aj, n]`ek, patp]na], and so on. There are many options that can be used in form element functions, such as i]tHajcpd]ÊÌÊÃiÌÊÌ
iÊ>ÝÕÊi}Ì
ÊvÊ>Ê/Ê>ÌÌÀLÕÌi°
>Ý
:hejg$]nn]u$# Helps ÌÊëvÞÊ>ÝÊÌ>ÃðÊÌÊÀiµÕÀiÃÊÌ
iÊÃÌ>ÌiiÌÊf]r]o_nelp) lnkpkpula#%%ÊÊÌ
iÊÛiÜ]ÊÜ
V
ÊÀiviÀiViÃÊÌ
iÊ*ÀÌÌÞ«iÊ>Û>-VÀ«ÌÊvÀ>iÜÀ]ÊÊ À`iÀÊÌÊÜÀÊ«À«iÀÞÊÊ>ÊÛiÜ°Ê9ÕÊV>Ê}iÌÊ>ÊV«ÞÊvÊ*ÀÌÌÞ«iÊvÀÊdppl6++sss* lnkpkpulafo*knc+`ksjhk]`.
>Û>-VÀ«Ì
Helps ÌÊëvÞÊ>Û>-VÀ«ÌÊÌ>ÃÃ]ÊÃÕV
Ê>ÃÊÌÊVÀi>ÌiÊ>Ê>Û>-VÀ«ÌÊ"LiVÌÊ Ì>ÌÊ :k^fa_p®°Ê/Ê>ÌÌ>V
Ê>ÊiÛiÌÊÌÊ>Ê -" ®ÊLiVÌÊvÀÊ>Ê>ÀÀ>Þ]ÊÕÃ}Êf]r]o_nelp) :arajp®° element, use f]r]o_nelp)
Paginator
Helps to format data into multiple pages or to sort data based on some parameters. ÀÊiÝ>«i]ÊÌÊVÀi>ÌiÊ>ÊÊÌÊÌ
iÊiÝÌÊÃiÌÊvÊ«>}>Ìi`ÊÀiÃÕÌÃ]ÊÕÃiÊÌ
iÊ l]cej]pkn):jatp$% function.
-iÃÃ
Provides vÕVÌÃÊÌÊ`i>ÊÜÌ
ÊÃiÃÃÊ>>}iiÌ°ÊÀÊiÝ>«i]ÊÌÊÀi`iÀÊ :bh]od®°Ê/ÊÀi>`Ê>ÊÛ>ÕiÃÊÃÌÀi`ÊÊ>Ê}ÛiÊÃiÃÃ]ÊÕÃiÊ messages, use oaooekj) oaooekj):na]`®°
/iÝÌ
Provides vÕVÌÃÊÌÊ`i>ÊÜÌ
ÊÌiÝÌÊÀÊÃÌÀ}Ê
>`}°ÊÀÊiÝ>«i]ÊÌÊÀiÛiÊ :pnei$% function. Ü
Ìië>ViÊvÀÊÌ
iÊLi}}Ê>`ÉÀÊi`ÊvÊÌiÝÌ]ÊÕÃiÊÌ
iÊpatp)
Time
Helps >>}iÊ`>ÌiÃÊ>`ÊÌiðÊÀÊiÝ>«i]ÊÌÊV
iVÊvÊ>Ê}ÛiÊ`>ÌiÉÌiÊÃÌÀ}ÊÃÊ :eoPk`]u$% function. today, use the peia)
8
:ahai$% Helps ÜÌ
Ê8Ê>«Õ>Ì°Ê/ÊVÀi>ÌiÊ8ÊiiiÌÃ]ÊÕÃiÊÌ
iÊtih) vÕVÌ°Ê/
ÃÊ
i«iÀÊV>Ê>ÃÊLiÊÕÃi`ÊÌÊVÛiÀÌÊ>ÊÀiÃÕÌÊÃiÌÊÌÊ8°
To reference the common helpers that you need in your application, you can specify the following statement in your =ll?kjpnkhhan class: r]n dahlano9]nn]u$#Dpih#(#Bkni#(#=f]t#(#F]r]O_nelp#%7
21
22
CH APT ER 1 N CAK E FU NDA MENTA L S
This will ensure that the f]r]o_nelp):hejg$% function in a layout works properly. 9ÕÊ>ÞÊii`ÊÌÊVÀi>ÌiÊÞÕÀÊÜÊ
i«iÀÊÀÊÌÜi>Ê>ÊiÝÃÌ}Ê
i«iÀÊV>ÃÃÊÌÊ«ÀÛ`iÊ >``Ì>ÊvÕVÌ>ÌÞÊÌ
>ÌÊÃÊÌÊÞiÌÊÃÕ««i`ÊLÞÊ >i°ÊÃÊ>ÊiÝ>«i]ÊÊÃÌ}Ê££nÊVÀi>ÌiÃÊ a simple helper called /]ll+reaso+dahlano+^na]g*ldl. This helper will print a variable and insert a new break after printing. Listing 1-18. A Sample Custom Break Helper _h]oo>na]gDahlanatpaj`o=llDahlanw bqj_pekjjasheja$ r]h%w napqnjpdeo):kqplqp$ r]h8^n+:%7 y y We can use this helper in our @al]npiajpo controller object to insert a break whenever we use the controller’s lnejp$%ÊvÕVÌ]Ê>ÃÊÃ
ÜÊÊÊÃÌ}Ê££°Ê Listing 1-19. Using the Sample Break Helper 8;ldl _h]oo@al]npiajpo?kjpnkhhanatpaj`o=ll?kjpnkhhanw r]n j]ia9#@al]npiajpo#7 r]n dahlano9]nn]u$#>na]g#%7 bqj_pekjlnejp$ r]h%w napqnjpdeo):>na]g):jasheja$ r]h%7 y y ;: ÀÃÌ]ÊÊÊÃÌ}Ê££]ÊÜiÊÀiviÀiViÊÌ
iÊLÀi>Ê
i«iÀÊLÞÊ`iV>À}Êr]n dahlano9 ]nn]u$#>na]g#%7°Ê iÝÌ]ÊÜiÊ`iviÊ>Êlnejp$% function that accepts a r]h parameter. This function contains the statement that invokes the break helper’s jasheja$ r]h% method, and consequently returns the result with a newline after it.
Plugins With Cake, you can create a complete MVC package called a plugin, which you can integrate into other Cake applications. A plugin is a mini-application with its own controllers, modiÃ]ÊÛiÜÃ]Ê>`ÊÌ
iÀÊ >iÊÀiÃÕÀViÃ°Ê >iÊ`iÃÊÌÊ
>ÛiÊ>ÞÊÊLÕÌÊÊ«Õ}ðÊ9ÕÊV>ÊÕÃiÊ third-party plugins, or better still, build your own.
C H A P T E R 1 N C A K E F U N D A M E N T A LS
Here, we will create a basic feedback plugin that will provide mailing facility. It will have the following directory structure: +]ll +lhqcejo +baa`^]_g +_kjpnkhhano +ik`aho +reaso +baa`^]_g[]ll[_kjpnkhhan*ldl +baa`^]_g[]ll[ik`ah*ldl where Ê
UÊ +_kjpnkhhano contains plugin controllers.
Ê
UÊ +ik`aho contains plugin models.
Ê
UÊ +reaso contains plugin views.
Ê
UÊ +baa`^]_g[]ll[_kjpnkhhan*ldl is the plugin’s =ll?kjpnkhhan, named after the plugin.
Ê
UÊ +baa`^]_g[]ll[ik`ah*ldl is the plugin’s =llIk`ah, named after the plugin.
NNote You must create both an =ll?kjpnkhhan and an =llIk`ah for a plugin to work properly. If you forget to define the Baa`^]_g=ll?kjpnkhhan class and the Baa`^]_g=llIk`ah, Cake will throw a “Missing Controller” error.
The feedback plugin’s =ll?kjpnkhhan is stored in ]ll+lhqcejo+baa`^]_g[]ll[_kjpnkhhan* ldl, and its corresponding =llIk`ah class is stored in ]ll+lhqcejo+baa`^]_g[]ll[ik`ah*ldl( as shown in ÊÃÌ}Ê£Óä° Listing 1-20. Feedback App Classes for the Feedback Plugin 8;ldl _h]ooBaa`^]_g=ll?kjpnkhhanatpaj`o=ll?kjpnkhhanw ++** y ;: 8;ldl _h]ooBaa`^]_g=llIk`ahatpaj`o=llIk`ahw ++** y ;:
23
24
CH APT ER 1 N CAK E FU NDA MENTA L S
Now, let’s create the Baa`^]_gOaj`?kjpnkhhan for our feedback plugin. The code in ÊÃÌ}Ê£Ó£ÊÃÊÃÌÀi`ÊÊ]ll+lhqcejo+baa`^]_g+_kjpnkhhano+baa`^]_g[oaj`[_kjpnkhhan*ldl. Listing 1-21. The FeedbackSendController to Invoke the send() Method 8;ldl _h]ooBaa`^]_gOaj`?kjpnkhhanatpaj`oBaa`^]_g=ll?kjpnkhhanw r]n j]ia9#Baa`^]_g#7 r]n qoao9]nn]u$#Baa`^]_g#%7 bqj_pekjoaj`$ pkAi]eh%w pdeo):oap$naoqhp(b]hoa%7 eb$pdeo):Baa`^]_g):oaj`Ai]eh$ pkAi]eh%%w pdeo):oap$naoqhp(pnqa%7 y y y ;: iÝÌ]ÊÜi½ÊVÀi>ÌiÊ>`ÊÃÌÀiÊÌ
iÊBaa`^]_gOaj`Ik`ah class in the ]ll+lhqcejo+baa`^]_g+ ik`aho+baa`^]_g[oaj`[ik`ah*ldlÊvi]Ê>ÃÊÃ
ÜÊÊÊÃÌ}Ê£ÓÓ° Listing 1-22. The FeedbackSendModel That Uses the PHP mail() Function to Send a Message 8;ldl _h]ooBaa`^]_gOaj`Ik`ahatpaj`oBaa`^]_g=llIk`ah w bqj_pekjoaj`Ai]eh$ na_eleajp%w ++Dana#osdanasapnupkoaj`]jai]ehiaoo]ca* eb$i]eh$ na_eleajp(#De#(#Sd]peo^]gejc#(#Bnki6oqc]n