Here’s a list of various CakePHP turorials, organized by subject. I’m trying to stick to mostly 1.2 stuff, but included some 1.1 tutorials that we’re still helpful. Leave a comment if I missed one and I’ll add it to the list.

General

manual.cakephp.org - The 1.1/Old Manual
tempdocs.cakephp.org - The 1.2/New Manual
www.amazon.com - Beginning CakePHP: From Novice to Professional (not yet released)
api.cakephp.org - The API

Getting Started

www.ibm.com - IBM’s 5-part “Cook up Web sites fast with CakePHP”
manual.cakephp.org - CakePHP Blog Tutorial
cakephp.bytenoise.co.uk - In the Kitchen with CakePHP

Version 1.2 Info

cakebaker.42dh.com - Deprecated stuff in CakePHP 1.2

Bake Console

cakebaker.42dh.com - Faster baking with bake
www.littlehart.net - Screencast: Interactive Console for CakePHP

Debugging

www.cakephp.org - Using Debug Messages

Test Suite

bakery.cakephp.org - Testing Models with CakePHP 1.2 test suite
cakebaker.42dh.com - How to use the official CakePHP test suite

Auth Component

www.littlehart.net - Simple User Registration in CakePHP 1.2
www.webdevelopment2.com - CakePHP Auth Component For Dummies Tutorial
www.webdevelopment2.com - CakePHP Auth Component - Tutorial Two

ACL

lemoncake.wordpress.com - Using AclBehavior in CakePHP 1.2
lemoncake.wordpress.com - Using AuthComponent and ACL in CakePHP 1.2
bakery.cakephp.org - How to use ACL with Cake PHP 1.2.x?
www.realm3.com - Setting Up User Groups With ACL and Auth in CakePHP 1.2

Ajax

bakery.cakephp.org - Getting started quickly with Scriptaculous effects

Advanced Ajax

blogs.bigfish.tv - Drag and drop using Ext JS with the CakePHP Tree Behavior
mentalramblings.info - CakePHP Ajax Comments

HABTM

tempdocs.cakephp.org - hasAndBelongsToMany (HABTM)

Admin Routing

www.cakephp.org - Admin Routing

Validation

bakery.cakephp.org - Multiple rules of validation per field in CakePHP 1.2
cakebaker.42dh.com - Validation with CakePHP 1.2
tempdocs.cakephp.org - Data Validation

Pagination

tempdocs.cakephp.org - Pagination
www.littlehart.net - CakePHP Pagination With A HABTM Relationship
cakebaker.42dh.com - Pagination of data from a HABTM relationship

RSS

cake.insertdesignhere.com - Cake 1.2’s new RSS helper

Theming

www.sanisoft.com - Theming your CakePHP apps (V1.2)
www.pseudocoder.com - Theming With CakePHP

Counter Cache

cakebaker.42dh.com - Counting associated records
www.cakephp.nu - CounterCache in CakePHP 1.2 beta

Builds

ertw.com - Pushing a CakePHP app from dev to prod

The Set Lib

www.thinkingphp.org - Cake 1.2’s Set class eats nested arrays for breakfast!
cakebaker.42dh.com - An alternative way to create arrays

Popularity: 100% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

This post is part 2 of a series focusing on CakePHP 1.2.

There have been a couple posts lately on the CakePHP Google Groups asking how themes work. I responded to one with the some quick basic steps. There is also a post by Tarique Sani that give a pretty good starting point.

This is basically a rehash of my response with a bonus tip to simplify theme management.

The Basics

In your AppController you turn on CakePHP theming by adding the following variables:

var $view = 'Theme';
var $theme = 'default';

I like to set the theme right off the bat to some default theme, then in the beforeRender callback reset the correct theme. How you store the current active them is up to you. It could be a config value, session value or a record in the database. Once you have this you can set it with:

$this->theme = $themeName;

Where do all the files go?

If you left all the CakePHP defaults alone you would put your layouts and views in /app/views/themed/themeName. For example the directory structure would look like this:

/app/views/themed/themeName
/app/views/themed/themeName/layouts/
/app/views/themed/themeName/layouts/default.ctp
/app/views/themed/themeName/views/
/app/views/themed/themeName/views/action/
/app/views/themed/themeName/views/action/index.ctp

Then for your css, js and images your would make similar directories under /app/webroot/themed:

/app/webroot/themed/themeName/js/
/app/webroot/themed/themeName/js/jquery.js
/app/webroot/themed/themeName/css/
/app/webroot/themed/themeName/css/style.css
/app/webroot/themed/themeName/img/
/app/webroot/themed/themeName/img/logo.jpg

I wasn’t particularly fond of this approach since it meant maintaining a theme in two locations. I couldn’t just “drop it in”.
Instead I chose to merge the two locations and have the views and layout located in /app/webroot/themed with the js, css and images. I’ll get to the security issues with this in a moment, but first how do I tell Cake to look in webroot for views? In your /app/config/bootstrap.php simply add:

$viewPaths = array(WWW_ROOT);

Now cake will look in both /app/views and /app/webroot/ for any views and themes.

The issue now is that all my views and layout are directly readable. If someone were to type in the path to one of these files in their browser they see all the raw PHP code. To stop this simply create a .htaccess file in /app/webroot/themed with:


DENY FROM ALL

Wait! My App Is Now Super Slow!

See the first comment for clarification on this issue.
Here’s what’s happening. The controller renderer has to load theme.php for outputting a themed view. It does this using the new App::import functinality, part of which is a recursive directory search through all the view paths, which after our change above looks like this:

/app/views
/app/webroot/
/cake/libs/view/

The theme.php files lives in the last location, but to find it, Cake has to search through the first two locations. Now if you have a ton of folders in your webroot, a bunch of folders for product images or something like FCKeditor (467 folders!), the search is going to take awhile.

To fix this I re-ordered the viewPath list in my AppController->beforeRender.

$viewPaths = Configure::read('viewPaths');
array_unshift($viewPaths, array_pop($viewPaths));
Configure::write('viewPaths', $viewPaths);

The list now looks like this:

/cake/libs/view/
/app/views
/app/webroot/

This probably isn’t the best solution, so if you find something better, please let me know.

Some Final Tips

The great thing about CakePHP theming is that if any of the views/css/js/whatever files don’t exist in the theme, Cake will automatically fall back to the default locations. This is nice since it allows you to only create the files you need and use the defaults for the rest of the site. In many cases you really only need theme files for the layout, css and images. I like to include two css files in each layout. A default.css which is in /app/webroot/css/ and has all the base styles for the site. Then a second style.css in /app/webroot/themed/themeName/css/ which has theme specific styles.

Elements work as normal with themed layout, so if you make the html code in them really generic (for example: a ul/li menu) it’s super easy to share the element, but make it look completely different with the theme’s css.

Popularity: 100% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

This post is part 1 of a series focusing on CakePHP 1.2.

CakePHP’s Interactive Bake Shell is one it’s most powerful features. It is a simplified command line interface for code generation. Most commonly it is used to generated code for models, views or controllers. I often use it to create a base of code, then tweak that code to fit into the application. An obvious issue is the baked code has actions and views for both add and edit functionality. Very often these two operations are virtually identical so, it doesn’t really make sense maintaining separate code that does the same thing, commonly know as DRY.

Merging the add and edit actions

This is actually quite simple and only take a minute to do. The steps are the same for both the regular add/edit and the admin routed add/edits.

The Add Action

Open the generated controller PHP file. In it you’ll see a function for add() and another for edit(). Replace the entire add function with a call to render the edit view. It should look like this:

	function add() {
		$this->render('edit');
	}

You could also completely remove this action and fix any link that point to /controller/add to /controller/edit, but I prefer this way, since code generated for other models may have links to this model if the tables are related. You can delete the /views/controller/add.ctp file.

The Edit Action

There are a few more changes required in the edit action. Find the edit function, right below the add function. Delete the first if statement:

		if (!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Model');
			$this->redirect(array('action'=>'index'), null, true);
		}

Further down these is:

if (empty($this->data)) {

Change that to read:

if ($id && empty($this->data)) {

The Edit View

As I mentioned above you can delete the add view (add.ctp). In the edit view (edit.ctp) you’ll need to add a parameter to the form create line to make sure the form always submits to the edit action. It should like something like this (your model will vary):

<?php echo $form->create('Model', array('url' => array('action' => 'edit'))); ?>

You may also want to change the legend so that it reflects whether you are adding or editing the record. There are a couple different ways to do this, but the quickest is to check the action:

<?php echo sprintf(__(’%s %s’, true), __(ucwords($this->action), true), __(’Model’, true));?>

That’s it. You now have an action and view that handles both add and edits.

Popularity: 94% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

Over the coming weeks I’m going to be writing a series of posts covering some advanced CakePHP topics. A basic understanding of CakePHP is assumed and, despite many complaints, there is a ton of good information out there for getting started with CakePHP.

In no particular order:
The “Old” Manual
The “New” Manual
The Official API
The Official Google Group
The unreleased Unofficial Book (Here’s the latest sample I could find)
IBM’s 5-part “Cook up Web sites fast with CakePHP”

The topics to be covered include:

If you’re interested sign up for the RSS feed. Look for the first post next week.

Popularity: 36% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit