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 function you write for a controller might be the index() function. When a request specifies a controller but not an action, the default CakePHP behavior is to execute the index() function of that controller. For example, a request for http://www.example.com/apples/ maps to a call on the index() function of the ApplesController, whereas http://www.example.com/apples/view/ maps to a call on the view() function of the ApplesController.
You can also change the visibility of controller functions in CakePHP by prefixing controller function names with underscores. If a controller function has been prefixed with an underscore, the function 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
}
}
?>
<?phpclass NewsController extends AppController {function latest() {$this->_findNewArticles();}function _findNewArticles() {//Logic to find latest news articles}}?>
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 function 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 map to a camelBacked URL retaining the plural form. For examples, RedApplesController (red_apples_controller.php) would map to http://example.com/redApples and OperatingSystemsController (operating_systems_controller.php) would map to http://example.com/operatingSystems.
