Access Controller instance in cakephp component

Source: Internet
Author: User
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) ;}}?>
  1. Class CheckComponent extends Object {
  2. // Called before Controller: beforeFilter ()
  3. Function initialize (& $ controller ){
  4. // Saving the controller reference for later use
  5. $ This-> controller = & $ controller;
  6. }
  7. // Called after Controller: beforeFilter ()
  8. Function startup (& $ controller ){
  9. }
  10. Function redirectSomewhere ($ value ){
  11. // Controller method
  12. $ This-> controller-> redirect ($ value );
  13. }
  14. }
  15. ?>

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) ;}}?>
  1. Class MyComponent extends Object {
  2. // Other components to be used
  3. Var $ components = array ('session ', 'math ');
  4. Function doStuff (){
  5. $ Result = $ this-> Math-> doComplexOperation (1, 2 );
  6. $ This-> Session-> write ('stuff ', $ result );
  7. }
  8. }
  9. ?>

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;}}?>
  1. Class MathComponent extends Object {
  2. Function doComplexOperation ($ amount1, $ amount2 ){
  3. Return $ amount1 + $ amount2;
  4. }
  5. Function doUberComplexOperation ($ amount1, $ amount2 ){
  6. $ UserInstance = ClassRegistry: init ('user ');
  7. $ TotalUsers = $ userInstance-> find ('count ');
  8. Return ($ amount1 + $ amount2)/$ totalUsers;
  9. }
  10. }
  11. ?>
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() {        // ...    }}?>
  1. Class CustomComponent extends Object {
  2. Var $ name = "Custom"; // the name of your component
  3. Var $ components = array ("Existing"); // the other component your component uses
  4. Function initialize (& $ controller ){
  5. $ This-> Existing-> foo ();
  6. }
  7. Function bar (){
  8. //...
  9. }
  10. }
  11. ?>
 Parent->bar();    }    function foo() {        // ...    }}?>
  1. Class ExistingComponent extends Object {
  2. Var $ name = "Existing ";
  3. Function initialize (& $ controller ){
  4. $ This-> Parent-> bar ();
  5. }
  6. Function foo (){
  7. //...
  8. }
  9. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.