Action Controllers

Introduction

Action controllers are "controller" part of Model-View-Controller [wikipedia.org]. They are loaded by the Front Controller when a request to the bootstrap is made. Every action controller must have at least one action. An action is simply a method in the action controller class. Here is an example action controller:

class foobar extends A_Controller_Action {

	public function index($locator) {
		// the default action
	}
	
	public function foo($locator) {
		// do something else
	}
	
	public function _bar() {
		// can't touch 'dis
	}

}

The first thing you probably notice is that the class extends the class A_Controller_Action. That's not necessary, but it does provide several conveniences. The comes from Skeleton's philosophy.

Provide link to skeleton's philosophy

Each action is given the Locator object that was passed to the Front Controller. This provides access to anything inside of the Locator, including the Request and Response objects, and anything else that was placed inside of it (e.g. Database connection, etc.).

If you create a method called index(), that will be used as the default action if no action is defined in the request. Tip: you can easily redirect the default action to another action by calling that method, passing the $locator argument.

Any method that begins with an underscore cannot be dispatched to. This means that it cannot be used as an action. This means you can create a method not meant to be dispatched to, without making it private.

Forwarding

Describe forwarding array

Customization

Many things laid out here can be customized with A_Controller_Mapper. See the documentation on Mapper.

Extending A_Controller_Action

Extending A_Controller_Action is handy, but by no means necessary. Here are some of the benifits:

Convenience Access

A_Controller_Action provides quick access to often-used items. For example, the Request and Response objects can be accessed from within an action with $this->request and $this->response, respectively. It also gives quick access to the controller Load helper with the _load() method ($this->_load()). Note that $this->request and $this->response will not be accessible unless the constructor is called with the Locator, either by not defining a constructor (as in the example above) or with parent::__construct($locator).

_request() is a quick way to get a value from the Request object, and _response() will set values to the Response object.

Other Methods

For the full list, see the PHPDocs.

Other Options

Either extending A_Controller_Action or not is not your only option. You can extend your own class to consolidate code common among controllers, or one of Skeleton's other controller classes.

List other specialty classes that can be extended