The widely unused and powerful setAction()

It has been quite a while since I last developed in Cake, but earlier today I spent a good 5+ hours working on some new controller internals for one of my apps. While working on this app, I thought I would give the not so used method, setAction() a spin. If you are unfamiliar with setAction(), it's a method that will internally forward one action to another. This is incredibly useful as it allows you to switch actions without having to do another HTTP request, nor having to reload all the controller and related objects; could be a huge saver on overhead.

Here's a quick example of a regular action:

public function profile($id) {
	$user = $this->User->findById($id);
	if (empty($user)) {
		$this->redirect(array('action' => 'listing'));
	}
	$this->set('user', $user);
}

What the action is doing is relatively simple. First it grabs a user based on an ID, checks to see if the result is empty, if so redirects to the users listing page, else sets the data. You could either redirect the action like I have, or you can simply throw an error message in the view saying something like "No user was found with that ID". I personally prefer redirecting in most cases, but it's a matter of preference. Lets move forward and try setAction() now.

public function profile($id) {
	$user = $this->User->findById($id);
	if (empty($user)) {
		return $this->setAction('listing');
	}
	$this->set('user', $user);
}

As you can see, I simply switched the redirect() with setAction(). Now if we visit a profile with an non-existent user, the listing action will be rendered without having to do another HTTP request. Also, do notice the use of return; this is extremely important because if you do not use return, all the logic after the setAction() will be ran and processed, causing unneeded overhead and unexpected results.

One downside that I have ran into with this approach is that the URL in the address bar does not change. But 99% of the time it won't matter as the page you render in its place will contain links to the correct pages or the user won't be copy and pasting the URL anyways.

Loading Models specific to certain actions

Recently I was working on a project, where a certain controller had over 15ish+ different models. The problem is that each action needed a different model, and I didn't want to load all those models at once. By using my common sense, loading that many models would put more of a strain on load time by having to initialize each model and bind them to the Controller. I needed a quick way to load a model(s) specific only to a certain action. At first I tried putting $this->user[] = array('Model'); at the top of each action, but that didn't work and it didn't initialize "before" the action like it is supposed to. So what I came up with is a little statement trick that you can place in your beforeFilter(), which will load models specific to certain actions. Enjoy!

/**
 * Executed before each action
 */
public function beforeFilter() {
	parent::beforeFilter();
	// Load action specific models
	switch ($this->params['action']) {
		case 'friends':		$models = array('Friend'); break;
		case 'messages':	$models = array('Message'); break;
		case 'blog':		$models = array('Entry', 'Comment'); break;
	}
	if (!empty($models)) {
		foreach ($models as $model) {
			$this->loadModel($model);
		}
	}
}

Also it would be best to disable models be default in controller. This way it doesn't load a certain model twice, or overwrite the defaults.

class TestController extends AppController {
	public $uses = null;
}