【问题标题】:Using a plugin component from shell class in cakephp 2.0.2在 cakephp 2.0.2 中使用 shell 类的插件组件
【发布时间】:2012-06-04 11:44:13
【问题描述】:

我想使用我的 shell 类中的插件组件。我正在尝试使用:

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

不幸的是,上述方法似乎失败了。

我收到一条错误消息,提示找不到 Component 类。

有什么建议我应该如何解决这个问题?

谢谢

【问题讨论】:

    标签: cakephp cakephp-2.0


    【解决方案1】:

    您应该始终先查看测试用例!这样你就会发现它们是通过这种方式手动包含的:

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

    和

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

    旁注: 如果您需要在控制器以外的地方使用组件而不是您做错了什么! 您应该将功能提取到模型或库中,并从组件和外壳中调用它

    【讨论】:

    • 感谢您的帮助标记。但是我不确定第二行。我做了这样的事情: App::uses('PokerAliasesWebService', 'Pluginname.Controller/Component');你能帮我解惑吗?谢谢。
    • sry,我从上面更正了我的陈述:它是App::uses('PokerAliasesWebServiceComponent', ...)
    • 好的,所以我的代码目前看起来像这样:App::uses('Component', 'Controller'); App::uses('PokerAliasesWebServiceComponent', 'Pluginname.Controller/Component'); $Collection = new ComponentCollection(); $this->PokerAliasesWebService = new PokerAliasesWebServiceComponent($Collection); 但我收到一条错误消息,提示找不到 ComponentCollection 类。
    • 您确实意识到Pluginname 应该是您的插件名称,对吧?我相应地编辑了我的帖子。
    • 嗨,马克,是的,我也试过了,但我仍然收到 ComponentCollection class not found 错误。
    【解决方案2】:

    我知道这已经很老了,但我只是在处理遗留的 CakePHP 2 应用程序时遇到了这个问题。

    在您的控制器中:

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

    然后您可以通过以下方式访问该组件:

    $this->Mycomponent->mymethod();
    

    Source

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2011-09-18
      • 2013-06-01
      • 1970-01-01
      • 2012-08-09
      • 2015-03-28
      相关资源
      最近更新 更多