Using a plugin component from shell class in cakephp 2.0.2
Asked Answered
R

2

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

Rosemarie answered 4/6, 2012 at 11:44 Comment(0)
M
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

Madai answered 4/6, 2012 at 11:58 Comment(8)
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.Rosemarie
sry, I corrected my statement from above: it is App::uses('PokerAliasesWebServiceComponent', ...)Madai
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.Rosemarie
you do realize that Pluginname should be your plugin name, right? I edited my post accordingly.Madai
Hi mark, yes I tried that too but I still get the ComponentCollection class not found error.Rosemarie
again, looking at the test cases helps: you will probably also need App::uses('Controller', 'Controller');Madai
I edited my answer to show how component collection can be added manuallyMadai
Alright that works :) ... Thanks for your help. Where do I find these test cases you mention?Rosemarie
S
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

Shearer answered 15/4, 2021 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.