CakePHP is an awesome MVC (Model-View-Controller) framework for PHP 4 and 5 - it provides a great base to build your web application on, and can save you a huge amount of time if used correctly.
Unfortunately, the manual is sometimes lacking - whilst the information you need might be there, you probably will not be able to find it because it’s hidden away in some random corner. I’m going to cover a few common questions (in my opinion) here.
How do I turn off the automatic database connection?
Sometimes you want a controller and model that don’t have any mapping to the database - for example, a static page controller. Cake complains unless you have a correctly named table in the database when you view the page.
Add this line to your model file to disable this feature:
var $useTable = false;
How do I turn off auto-layout rendering?
Cake also automatically lays out your controller methods into the default layout - you might want to turn this off if the controller deals with AJAX calls and returns simple responses, or simply if you want to make use of print and print_r.
At the top of your controller, inside the class, add:
var $autoRender = false;
You can also disable this per method, as far as I know.
Additionally, Cake comes with a AJAX layout in the core that returns the template file without the default layout. You can use it by specifying the render function in the last line of your method (e.g. for function myAction()):
$this->render('myAction', 'ajax');
Where do I put application-wide code?
Create a file called app_controller.php in your app directory:
project/app/app_controller.php
<?php
class AppController extends Controller {
//code
} //AppController
?>
This file overrides the default app controller that each of your controller’s extends, thus you can add any code here that you want all of your controllers to see.









I have heard of Cakephp before, and have never had a look, so that’s what is guna keep me up until sunrise this morning, I BLAME YOU
The documentation for CakePHP is spread out all over the place, so I’m glad I found your post. It’s a really fabulous framework - if they ever get the documentation centralized a bit better I think it would be used even more widely than it already is.
good tips. thx
49g5×9ilgszbxsxw
@george: You’re welcome to help out. The Book (http://book.cakephp.org) is editable & open to all; feel free to submit whatever improvements you think might be beneficial.
Great tip / advice… ^o^ Keep up the good work!… >o<