【问题标题】:CakePHP 2.x - Using Plugins with a Shell CommandCakePHP 2.x - 通过 Shell 命令使用插件
【发布时间】:2018-01-23 06:30:06
【问题描述】:

我目前正在尝试为 CakePHP 2.x 使用以下 Stripe 插件:

https://github.com/chronon/CakePHP-StripeComponent-Plugin

我创建了一个尝试利用 Stripe 库的功能之一的 Shell 命令。

我的引导程序如下:

App::import('Vendor', array('file' => 'autoload'));
CakePlugin::load('Stripe');

我在 Shell 命令的头部定义了以下内容:

App::uses('Shell', 'Console');
App::uses('Controller', 'Controller');
App::uses('ComponentCollection', 'Controller');
App::uses('StripeComponent', 'Stripe.Controller/Component');

最后,在main函数中,我写了以下内容:

$collection = new ComponentCollection();
$StripeComponent = new StripeComponent($collection);
$controller = new Controller();
$StripeComponent->initialize($controller);
$sub = $StripeComponent->stripeFunction($data);

我在执行命令时,出现如下错误:

PHP 致命错误:在第 367 行的 /var/www/html/myapp/app/Plugin/Stripe/Controller/Component/StripeComponent.php 中找不到类“Stripe” 致命错误:在第 367 行的 /var/www/html/myapp/app/Plugin/Stripe/Controller/Component/StripeComponent.php 中找不到类“Stripe” 致命错误错误:在 [/var/www/html/myapp/app/Plugin/Stripe/Controller/Component/StripeComponent.php,第 367 行] 中找不到类“Stripe”

该错误似乎是导致StripeComponent中这一行出现问题,这似乎表明该插件在命令执行期间没有加载:

Stripe::setApiKey($this->key);

似乎在执行命令时插件未正确加载(该插件适用于应用程序的其余部分)。我做错了什么会影响插件的加载吗?或者shell命令不能在执行之前加载所有插件?

任何方向都非常感谢。

【问题讨论】:

  • 澄清一下,你安装Stripe's PHP library了吗?
  • 文档中说,你需要安装Stripe PHP库。你需要组件(打包为插件),以及Stripe的PHP库。
  • 感谢 Shahonseven,文档确实这么说。我会调查下一次出现在机器前的时间,并在需要时进行更新。
  • 不客气...
  • @shahonseven Stripe PHP 库作为插件的一部分包含在内,Stripe PHP 库代码包含在 myapp/app/Plugin/Stripe/Vendor/Stripe 中。

标签: php shell cakephp plugins


【解决方案1】:

事实证明,我需要调用启动方法而不是初始化:

$collection = new ComponentCollection();
$StripeComponent = new StripeComponent($collection);
$controller = new Controller();
//$StripeComponent->initialize($controller);
$StripeComponent->startup($controller);
$sub = $StripeComponent->stripeFunction($data);

为了回答最初的问题,Shell 命令确实允许访问插件库代码。

【讨论】:

  • 那应该重构,组件肯定是放那个代码的地方放错了地方。
猜你喜欢
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多