我是yii的新手,我必须为Enomapi创建一个yii组件。它在corephp中,我想把它作为组件或模块在yii中实现
-
将文件接口和类放在yii组件文件夹中。
-
按照这里提到的修改类yii自定义组件。现在我的类名是
EnomService
,接口名是EnomInterface
我在我的班级中也添加了这些行使用Yii;使用yii''base''Component;使用yii''base''InvalidConfigException;
-
修改了config文件夹中的
main.php
文件:'import'=>array( 'application.models.*', 'application.components.*', ), 'defaultController'=>'post', // application components 'components'=>array( 'user'=>array( // enable cookie-based authentication 'allowAutoLogin'=>true, ), 'mycomponent' => [ 'class' => 'app'components'EnomService', ],
-
以这种方式调用控制器。
public function actionEnom () { echo "asdgsgsag"; $enom = new EnomService('manoj_rudra', 'manoj@41@', false, true); $enom->debug = true; $result= Yii::$app->EnomService->checkDomain('systurn', 'com', true); //$result = $enom->checkDomain('systurn', 'com', true); // This enables domain spinner echo '<pre>'; var_dump($result); echo '</pre>'; }
但它不起作用。我对yii自定义组件不太熟悉。请帮我创建这个。
您使用的是Yii还是Yii2?
如果是Yii,那么你可以使用大量其他现有的扩展来激励自己,例如这个:https://github.com/HeavyDots/yii-sms
至于Yii2,你可以做一些类似的事情,看看YiiFramework网站上已经存在的Yii2扩展,你可以看到组件类是如何定义的。
我建议:
1) 在名为"enom"的"组件"中创建一个新目录
2) 将所有来自的enom文件放在该目录中https://github.com/comdexxsolutionsllc/MoondayFramework/tree/master/engine/enom
3) 在目录中创建名为"Enom.php"的组件类,如下所示:
<?php
// include enom service class
require(dirname(__FILE__).'/class.EnomService.php');
namespace components'enom;
use Yii;
class Enom extends 'yii'base'Component
{
// define private property to store service
private $service;
public function init()
{
parent::init();
// init the service
$this->service=new EnomService('manoj_rudra', 'manoj@41@', false, true);
}
/**
* @return EnomService
*/
public function getService() {
return $this->service;
}
}
?>
4) 然后在配置中正确定义组件
'enom' => [
'class' => 'app'components'enom'Enom',
],
5) 最后像这个一样使用它
Yii::$app->enom->getService()->checkDomain
正如我之前所说,我还没有使用Yii2,所以这可能需要调整,但可能会让你走上正确的道路。