3

in app_controller.php load Session, Auth components.

And in posts_controller.php, i use CustomComponent with $components = array('Session','Auth');

Then do CustomComponent must reload Session, Auth components?

if i use and create much components and that components use other components .It will make app is very slow?


I asked in cakephp IRC, A person answer is not:

[11:05] it will not be slow, I believe it passes those around via reference

[11:05] so you have nothing to worry about

meotimdihia
  • 4,191
  • 15
  • 49
  • 69

3 Answers3

8

Lets say you want to import BComponent into AComponent.

AComponent

class AComponent extends Component {
     public $components = array('BComponent');

     public function xyz(){
           $test = $this->BComponent->abc($name);
           echo $test;
     }
}

BComponent

class BComponent extends Component {

     public function abc($name){
           return "My name is: ". $name;
     }
}
Fury
  • 4,643
  • 5
  • 50
  • 80
  • How does that work if you need to pass a configuration array into BComponent which is interpenetrated in a initialize() call for BComponent? – space97 Dec 18 '20 at 23:37
3

yes, it would need to be $components = array('Session','Auth','Custom'); Or you can use: App::import('Component', 'Custom');$Custom = new CustomComponent();

Then do CustomComponent must reload Session, Auth components? If you don't use Session or Auth inside your CustomComponent class, then no.

It will make app is very slow? No, unless you use a lot of components.

Anh Pham
  • 5,431
  • 3
  • 23
  • 27
0

I tried another answers here but this is the only way it works for me

Imagine that we have two components

1 - IntegrationsComponent

2 - ProcessComponent

And we want to use IntegrationsComponent inside ProcessComponent, so in this last component we have to call the component using this way

App::uses('ComponentCollection', 'Controller');
App::uses('Controller', 'Controller');
App::uses('IntegrationsComponent', 'Controller/Component'); 

class ProcessComponent extends Component {

// Method of this component where we call another component method inside of it
    
public function methodForCallingAnotherComponent() {
    
    // Initialize component
      
    $collection = new ComponentCollection();
    $this->IntegrationsComponent = new IntegrationsComponent($collection);
    
    // You can pass properties' component if it has
    
    $this->IntegrationsComponent->create = false;
    $this->IntegrationsComponent->type = 'UPDATE_PRODUCT';
                    
    // Call method of the component
    $this->IntegrationsComponent->ecommercePublish();
    
    }
    
}