2

I would like to make use of a plugin component from my shell class. I am trying to use:

App::import('Component', 'Myplugin.Mycomponent');
$this->Mycomponent =& new MycomponentComponent();

Unfortunately the above seems to fail.

And I get an error message saying that the Component class could not be found.

Any suggestion how I should tackle this?

Thanks

Gabriel Spiteri
  • 4,896
  • 12
  • 43
  • 58

2 Answers2

4

you should always take a look at the test cases first! this way you would have found out that they are manually included this way:

App::uses('ComponentCollection', 'Controller');
App::uses('AppleComponent', 'Myplugin.Controller/Component');

and

$Collection = new ComponentCollection();
$Apple = new AppleComponent($Collection);

sidenote: if you need to use a component somewhere else than a controller than you are doing sth wrong! you should extract the functionality into a model or lib and call it from both the component and the shell

mark
  • 21,691
  • 3
  • 49
  • 71
  • thanks for you help mark. However I am unsure about the second line. I did something like this: App::uses('PokerAliasesWebService', 'Pluginname.Controller/Component'); Could you help me undestand? Thank you. – Gabriel Spiteri Jun 04 '12 at 12:16
  • sry, I corrected my statement from above: it is `App::uses('PokerAliasesWebServiceComponent', ...)` – mark Jun 04 '12 at 12:29
  • ok so my code currently looks like this: `App::uses('Component', 'Controller'); App::uses('PokerAliasesWebServiceComponent', 'Pluginname.Controller/Component'); $Collection = new ComponentCollection(); $this->PokerAliasesWebService = new PokerAliasesWebServiceComponent($Collection);` but I get an error saying the ComponentCollection class could not be found. – Gabriel Spiteri Jun 04 '12 at 12:39
  • you do realize that `Pluginname` should be your plugin name, right? I edited my post accordingly. – mark Jun 04 '12 at 12:53
  • Hi mark, yes I tried that too but I still get the ComponentCollection class not found error. – Gabriel Spiteri Jun 04 '12 at 13:18
  • again, looking at the test cases helps: you will probably also need `App::uses('Controller', 'Controller');` – mark Jun 04 '12 at 13:25
  • I edited my answer to show how component collection can be added manually – mark Jun 04 '12 at 13:47
  • Alright that works :) ... Thanks for your help. Where do I find these test cases you mention? – Gabriel Spiteri Jun 04 '12 at 13:51
0

I know this is hella old, but I just ran into this while working on a legacy CakePHP 2 application.

In your controller:

public $components = array(
    'Myplugin.Mycomponent',
);

You can then access the component via:

$this->Mycomponent->mymethod();

Source

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57