jeeeee jeeeee - 1 year ago 101
PHP Question

CakePHP $this Syntax

I've been trying to learn CakePhp for a while now but I still can't get alot of stuff. I've been reading a lot and watching videos. I just want to ask a very simple question.

I've been trying to mess with the bookmarks tutorial and i'm watching a video. In the video he baked a component called Validate. In the cmd he typed

bin/cake/bake component Validate

Then a ValidateComponent.php appeared in the component folder in the controller. Now he used the ValidateComponent.php by going to the BookmarksController and adding to the initialize method


I just want to ask where did the word validate come from? shouldn't it be ValidateComponent? and where does he get the loadComponent from? I've seen him using
$this->method('string', [array]);
I just want to know how the syntax works and what each word means. Sorry for the long explanation. I'm really trying to learn and i'm really confused. thank you very much.


namespace App\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;

* Validate component
class ValidateComponent extends Component

* Default configuration.
* @var array
protected $_defaultConfig = [];

public function validLimit($limit, $ default)
if (is_numeric($limit)){
return $limit;
return $default;


part of BookmarksController.php

public function initialize()



I can't seem to find where he got the word 'Validate'

Answer Source

Every controller in your application extends a base Controller Controller or AppController which extends Controller

Controler have bunch of methods, One of these methods is the loadComponent() (See Source)

public function loadComponent($name, array $config = [])
         list(, $prop) = pluginSplit($name);
         $this->{$prop} = $this->components()->load($name, $config);

Why Validate instead of ValidateComponent?

Short answer: suffix.

See predefined suffix in App class

CakePHP uses suffix to load classes, When you hit loadComponent() You go to ComponentRegistery class to Register the component, ComponentRegistery will call App class to load class. __loadClass()

Almost everything in CakePHP has a suffix, In your case ValidateComponent has the Component suffix.

return App::className($class, 'Controller/Component', 'Component'); (Source)

I hope this will make more sense to you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download