CakePHP has a number of core components that can be used with an application (email, cookie, session, etc). One of the controllers that I am currently using on a project calls a custom component. (I often use controllers that do not have an associated Data Model, but use custom components instead). Setting up a custom component is rather simple.
Creating The Component
Components extend the Object Class. Be sure to use CamelCase naming convention. All components are suffixed with ‘Component’.
class DomainSearchComponent extends Object { function checkDomain() { } } |
The structure of the component is the same as any other PHP class.
The component file is stored under app/controllers/components.
Using the Component
To use the Component in a controller, you first have to let CakePHP know that it exists.
class DomainnamesController extends AppController { var $components = array('DomainSearch'); } |
Now you can call a component method from within the controller.
class DomainnamesController extends AppController { var $components = array('DomainSearch'); function search() { return $this->DomainSearch->checkDomain(); } } |
That’s it.