Search code examples
phpcakephproutesconventions

How does Cakephp Checks its naming conventions and folder names?


Where does the cakephp naming conventions (i.e controller name should be CakesController.php i.e end with Controller) and the folder conventions (i.e controller files are from app/Controller folder) are defined . i.e how does cakephp check it. in which file they are defined.


Solution

  • Most of that stuff happens in the Dispatcher with the help of the Router and the Inflector, for instance:

    protected function _loadController($request) {
        $pluginName = $pluginPath = $controller = null;
        if (!empty($request->params['plugin'])) {
            $pluginName = $controller = Inflector::camelize($request->params['plugin']);
            $pluginPath = $pluginName . '.';
        }
        if (!empty($request->params['controller'])) {
            $controller = Inflector::camelize($request->params['controller']);
        }
        if ($pluginPath . $controller) {
            $class = $controller . 'Controller';
            App::uses('AppController', 'Controller');
            App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
            App::uses($class, $pluginPath . 'Controller');
            if (class_exists($class)) {
                return $class;
            }
        }
        return false;
    }
    

    Source: https://github.com/cakephp/cakephp/blob/master/lib/Cake/Routing/Dispatcher.php#L244

    Also see the CakePHP Cookbook explaining a typical request

    typical CakePHP Request Lifecycle