Article
The CakePHP Framework: Your First Bite
FAQs about CakePHP's Additional Features
CakePHP offers a lot of features that cannot properly be described in a single article. However, I've included a shortlist of frequently asked questions that may help you to understand this framework further.
1. How can I make my application more secure?
The examples in this article are inherently insecure. Luckily, CakePHP comes with a Sanitize class, which can be used in Cake applications to filter strings or arrays to make them safe for display or insertion into the database.
More information about sanitizing can be found in the CakePHP manual.
Regarding validation, it's possible to make sure that the entered data satisfies particular rules or patterns by adding some validation rules to our model, like this:
<?php
class Note extends AppModel
{
var $name = 'Note';
var $validate = array(
'title' => VALID_NOT_EMPTY,
'body' => VALID_NOT_EMPTY
);
}
?>
VALID_NOT_EMPTY is a constant defined in /cake/libs/validators.php, and can be used to make sure that a particular field is not left blank. CakePHP comes with some predefined constants, but custom constants can be created.
After you define validation rules, all relevant actions and views should be modified accordingly. More information and examples are available in these pages of the manual.
2. Is there any way to turn off Cake's 'debugging mode'? Is there a main configuration file?
Yes. A main configuration file, which governs some of CakePHP's core settings, is located in /app/config/core.php. Some of the settings that can be modified via this file include:
- CakePHP's debugging verbosity and type
- logging level
- cookies and session duration
- session storage location
3. All the business logic should go in my controllers, but what if I want to re-use something elsewhere?
Good question. You will almost always have to create some complex logic for an application, and you usually want to re-use part of that logic. The most common way to include an application-wide function or variable so that it's available in every controller is to define it in your AppController file. This file basically consists of an empty class that extends Cake's internal Controller class, and is located in the /cake/ directory. You can move it to your /app/ directory and create methods that will be available in all of your custom controllers that extend AppController. Even if you're not planning to use an AppController at first, it's often wise to create custom controllers which extend AppController rather than the Controller class.
An easy way to create custom classes handling a specific task is to create a component. Components can be loaded automatically in controllers (and only inside controllers) by adding a variable named $components:
var $components = array('Session', 'MyCustomComponent');
CakePHP comes with some default components such as Session, which offers convenient ways to organize session data, or RequestHandler, which can be used to determine more information about HTTP requests. These are documented in the CakePHP manual:
4. Does CakePHP require PHP5?
No. CakePHP is 100% compatible with PHP4. Personally, I think this is one of Cake's main strengths. For example, the __construct() method can be used on PHP4 on all classes extending the Object core class, which is to say nearly everything in CakePHP. Similar patches have been included in the core libraries to offer additional functionality in PHP4 as well. Unfortunately, variables and methods don't support access modifiers, and a private method should be prefixed with an underscore. This is not just a convention: in a controller, it really means that the method is private. If someone tries to access it (e.g. via http://localhost/notes/_privatemethod/), Cake will return an error.
5. What are CakePHP's default helpers?
CakePHP comes with some very handy helpers that can really make your life easier when it comes to creating views:
- HTML -- allows quick creation of HTML tags, including links and input fields
- JavaScript -- offers an easy way to manage JavaScript code
- Number -- a set of useful methods to format numeric data
- Time -- functions to format time strings and timestamps
- Text -- auto-link URLs, truncate strings, create excerpts, highlight, strip links and more
- AJAX -- a truly amazing AJAX helper, to be used in conjunction with the popular Prototype and script.aculo.us libraries; this helper can really speed up the creation of AJAX interfaces
More information about helpers is available in the CakePHP manual.
6. Is there any way to include my custom function/class in Cake?
Sure there is. If you want to use a custom external class, you can put it in the /vendors/ directory and load it into your controller like this:
vendors('MyClassName');
If you need to define custom application-wide constants or functions, you can place them in /app/config/bootstrap.php, which will make them available everywhere in your application.
You can adapt your code and create a helper or a component to be used in conjunction with views or controllers.
You can also try to integrate other software packages into Cake. An example? Check out the CakeAMFPHP project.
7. What if I need to work with more than one table simultaneously?
By default, a NotesController will try to locate and load a Note model class. If your controller needs to access more than its default model, you can define additional models by setting the $uses array, like this:
var $uses = array(Note, AnotherModel, YetAnotherModel);
In some cases, two or more tables might be closely related and would therefore be used with JOIN statements: your notes may have been submitted by different people listed in an authors table, for example. In these cases, CakePHP's Associations can be used to define complex table relationships directly in your Model class. More information is available in these manual pages.
8. Is it possible to further customize my application's URLs?
Yes. Check out the /app/config/routes.php file, and feel free to define or modify your custom routes. For example:
$Route->connect ('/', array('controller'=>'notes', 'action'=>'index'));
This creates a default route for http://localhost/ to:
http://localhost/notes/index/.
9. Is there an authentication mechanism in Cake?
Yes and no. There's no official authentication component, simply because needs can be very different depending on the type of application being developed. There is, however, a built-in Access Control List mechanism involving flat files or databases. More information can be found in these manual pages.
CakePHP Resources
The CakePHP Project is continuously growing: as more and more users start using the framework and creating their own projects, the documentation continues to improve. As such, more and more web sites and blogs are developing a lot of useful information that they're making freely available to CakePHP "bakers".
Here's a shortlist of various places featuring Cake-related material:
CakePHP Wiki -- a community-powered wiki with various Cake tutorials and how-tos
The CakePHP Manual -- CakePHP's official manual, which is still a work in progress, but already is fairly comprehensive
CakePHP Google user group -- a very lively user group; if you have a question to ask, go here
Official CakePHP IRC channel: #cakephp on irc.freenode.net -- chat with other bakers, as well as CakePHP's creators, in real time
CakeForge -- the perfect place to host and share your open source CakePHP-related projects
Documentation for offline use
Summary
CakePHP is a mature framework for PHP developers who want the structure and time-saving benefits of Ruby on Rails, without having to leave their comfort zone or get their head around obscure Ruby syntax. Using Cake's scaffolding, it's possible to build a prototype application quickly, using a minimal amount of code. And, with a large number of helper classes available to extend and customize your application while retaining a sensible and easily maintainable architecture, Cake makes the possibilities endless. CakePHP is being actively developed, and is backed by extensive documentation and a lively support community.
This article has given you a taste of what's possible with CakePHP. Now it's time for you to go off and do a little baking of your own!