Yii基础
Yii实现Soap服务
创建Yii项目
// 在/home/work/codes 目录下,创建名为 yii_demo的项目
Yii目录/framework/yiic webapp /home/work/codes/yii_demo
创建 apache配置文件:
cd /home/work/yii_demo/protected
mkdir site-conf
vi yii_demo.conf
Listen 8777
<VirtualHost *:8777>
ServerAdmin lizhenkun01@baidu.com
DocumentRoot /home/work/codes/yii_demo/
<Directory "/home/work/codes/yii_demo/">
RewriteEngine on
RewriteRule . index.php
</Directory>
</VirtualHost>
将配置文件加入到apache服务
vi apache2目录/conf/httpd.conf
// 在文件最后增加
Include /home/work/codes/yii_demo/protected/site-conf/yii_demo.conf
重启apache服务
apachectl -k restart
创建Soap服务
创建yii_demo/protected/controller/SoapDemoController.php,代码如下:
<?php
/**
* YII框架API接口端的server (基于Webservice的接口服务器端)
* 注意php.ini开启soap扩展:extension=php_soap.dll (Windows)/php_soap.so (Linux)
*/
class SoapDemoController extends CController {
public function actions() {
// 必须实现的函数
return array(
'service'=>array(
'class'=>'CWebServiceAction',
),
);
}
/**
* 方法成为soap服务的两个必要条件: 增加@return 和 @soap注释,并且 @return 必须声明返回类型
* @return string
* @soap
*/
public function demoString() {
return 'demo string';
}
/**
* @return array
* @soap
*/
public function demoSimpleArray() {
return array(
'Name'=>'lizhenkun01',
'Age'=>18,
);
}
/**
* @return array
*/
public function demoNotSoap() {
return array(
'Name'=>'lizhenkun01',
'Age'=>18,
);
}
}
浏览器测试:
http://localhost:8777/soapDemo/service
代码测试,测试代码如下:
vi soap_test.php (任意目录)
<?php
$client = new SoapClient('http://localhost:8777/soapDemo/service');
var_dump($client->demoSimpleArray());
$client = new SoapClient('http://localhost:8777/soapDemo/service');
var_dump($client->demoString());
命令行调用Yii
- 在 protected/commands 中,创建 XyzCommand.php
- 在 XyzCommand.php 中 实现 class XyzCommand,且类要继承CConsoleCommand,类中实现 run 函数
命令行运行 XyzCommand: ./protected/yiic xyz
例如:
./protected/yiic
可以看到:Yii command runner (based on Yii v1.1.8) Usage: ./yiic <command-name> [parameters...] The following commands are available: - message - migrate - shell - webapp
按上述方法实现:
vi protected/commands/HelloCommand.php <?php class HelloCommand extends CConsoleCommand { public function run($args) { echo("Hello Command\n"); } } ?>
再执行./protected/yiic
可以看到:Yii command runner (based on Yii v1.1.8) Usage: ./yiic <command-name> [parameters...] The following commands are available: - hello - message - migrate - shell - webapp
执行: ./protected yiic hello
命令行输出: Hello Command