{EN} - 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
	}
}

?>
  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 function is preceded with an underscore.

{EN} - 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
	}
}

?>
  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 function is preceded with an underscore.

Differences