If you want to access the controller instance in the component, you need to implement the initialize () or startup () method of the component. These two special methods receive a reference to the controller as the first parameter and are automatically called. The initialize () method is automatically called before the beforeFilter () method of the controller is executed.
If you want to access the controller instance (InstanceTo implement the initialize () or startup () method of the component. These two special methods receive a reference to the controller as the first parameter and are automatically called. The initialize () method is automatically called before the beforeFilter () method of the controller is executed. the startup () method is automatically called after the beforeFilter method is executed. If you do not want the startup () method to be called when the controller executes the build operation for some reason, you can set the class member variable $ disableStartupTrue.
The initialize () method is the most suitable option if you want to do something before the controller's beforeFilter.
Controller = & $ controller;} // function startup (& $ Controller) {} function redirectSomewhere ($ value) is called after controller: beforeFilter) {// how to use the controller $ this-> controller-> redirect ($ value) ;}}?>
- Class CheckComponent extends Object {
- // Called before Controller: beforeFilter ()
- Function initialize (& $ controller ){
- // Saving the controller reference for later use
- $ This-> controller = & $ controller;
- }
- // Called after Controller: beforeFilter ()
- Function startup (& $ controller ){
- }
- Function redirectSomewhere ($ value ){
- // Controller method
- $ This-> controller-> redirect ($ value );
- }
- }
- ?>
Sometimes you may need to use other components in the component. you only need to declare the class member variable $ components (just like in the controller) in the component ), its value is an array of component names you want to use.
Math-> doComplexOperation (1, 2); $ this-> Session-> write ('stuff ', $ result) ;}}?>
- Class MyComponent extends Object {
- // Other components to be used
- Var $ components = array ('session ', 'math ');
- Function doStuff (){
- $ Result = $ this-> Math-> doComplexOperation (1, 2 );
- $ This-> Session-> write ('stuff ', $ result );
- }
- }
- ?>
Use or access models in components (Model) Is not recommended. However, this is not the case. in this case, you need to manually generate an instance of the model for use. The following is an example:
find('count');return ($amount1 + $amount2) / $totalUsers;}}?>
- Class MathComponent extends Object {
- Function doComplexOperation ($ amount1, $ amount2 ){
- Return $ amount1 + $ amount2;
- }
- Function doUberComplexOperation ($ amount1, $ amount2 ){
- $ UserInstance = ClassRegistry: init ('user ');
- $ TotalUsers = $ userInstance-> find ('count ');
- Return ($ amount1 + $ amount2)/$ totalUsers;
- }
- }
- ?>
See comments for this section3.6.3.3 Using other Components in your Component
- Translate
- View just this section
- Comments (0)
- History
There is no translation yet for this section. Please help out and translate this .. More information about translations
Sometimes one of your components may need to use another.
You can include other components in your component the exact same way you include them in controllers: Use the $ componentsvar.
Existing->foo(); } function bar() { // ... }}?>
- Class CustomComponent extends Object {
- Var $ name = "Custom"; // the name of your component
- Var $ components = array ("Existing"); // the other component your component uses
- Function initialize (& $ controller ){
- $ This-> Existing-> foo ();
- }
- Function bar (){
- //...
- }
- }
- ?>
Parent->bar(); } function foo() { // ... }}?>
- Class ExistingComponent extends Object {
- Var $ name = "Existing ";
- Function initialize (& $ controller ){
- $ This-> Parent-> bar ();
- }
- Function foo (){
- //...
- }
- }