Welcome to the Cookbook

loading...

2.4.3 Controller Conventions

Controller classnames are plural, CamelCased, and end in Controller. PeopleController and LatestArticlesController are both examples of conventional controller names.

The first method you write for a controller might be the index() method. When a request specifies a controller but not an action, the default CakePHP behavior is to execute the index() method of that controller. For example, a request for http://www.example.com/apples/ maps to a call on the index() method of the ApplesController, whereas http://www.example.com/apples/view/ maps to a call on the view() method of the ApplesController.

You can also change the visibility of controller methods in CakePHP by prefixing controller method names with underscores. If a controller method has been prefixed with an underscore, the method will not be accessible directly from the web but is available for internal use. For example:

<?php
class NewsController extends AppController {

	function latest() {
		$this->_findNewArticles();
	}
	
	function _findNewArticles() {
		//Logic to find latest news articles
	}
}

?>
  1. <?php
  2. class NewsController extends AppController {
  3. function latest() {
  4. $this->_findNewArticles();
  5. }
  6. function _findNewArticles() {
  7. //Logic to find latest news articles
  8. }
  9. }
  10. ?>

While the page http://www.example.com/news/latest/ would be accessible to the user as usual, someone trying to get to the page http://www.example.com/news/_findNewArticles/ would get an error, because the method is preceded with an underscore.

2.4.3.1 URL Considerations for Controller Names

As you've just seen, single word controllers map easily to a simple lower case URL path. For example, ApplesController (which would be defined in the file name 'apples_controller.php') is accessed from http://example.com/apples.

Multiple word controllers can be any 'inflected' form which equals the controller name so:

  • /redApples
  • /RedApples
  • /Red_apples
  • /red_apples

will all resolve to the index of the RedApples controller. However, the convention is that your urls are lowercase and underscored, therefore /red_apples/go_pick is the correct form to access the RedApplesController::go_pick action.

For more information on CakePHP URLs and parameter handling, see Routes Configuration.