Registry Objects

The registry classes provide a simple way to create and retrieve loadedinstances of a given object type. There are registry classes for Components,Helpers, Tasks, and Behaviors.

While the examples below will use Components, the same behavior can be expectedfor Helpers, Behaviors, and Tasks in addition to Components.

Loading Objects

Objects can be loaded on-the-fly using add<registry-object>()Example:

  1. $this->loadComponent('Acl.Acl');
  2. $this->addHelper('Flash')

This will result in the Acl property and Flash helper being loaded.Configuration can also be set on-the-fly. Example:

  1. $this->loadComponent('Cookie', ['name' => 'sweet']);

Any keys and values provided will be passed to the Component’s constructor. Theone exception to this rule is className. Classname is a special key that isused to alias objects in a registry. This allows you to have component namesthat do not reflect the classnames, which can be helpful when extending corecomponents:

  1. $this->Auth = $this->loadComponent('Auth', ['className' => 'MyCustomAuth']);
  2. $this->Auth->user(); // Actually using MyCustomAuth::user();

Triggering Callbacks

Callbacks are not provided by registry objects. You should use theevents system to dispatch any events/callbacksfor your application.

Disabling Callbacks

In previous versions, collection objects provided a disable() method to disableobjects from receiving callbacks. You should use the features in the events system toaccomplish this now. For example, you could disable component callbacks in thefollowing way:

  1. // Remove Auth from callbacks.
  2. $this->eventManager()->off($this->Auth);
  3.  
  4. // Re-enable Auth for callbacks.
  5. $this->eventManager()->on($this->Auth);