PHP命令传递参数,Yii下命令$args来源

作者:袖梨 2022-06-25

在使用YII框架的时候,在架构该框架之后会在protected文件夹下生成一个yiic命令工具,在这里你可以使用YII自带的命令,首先你需要现在commands下新建一个command命令php文件,文件名格式如DemoCommand.php

代码如下:

代码如下 复制代码

class DemoCommand extends CConsoleCommand{
//进程数
private $thread = 10;
public function run($args)
{
$demos= $this->getDemo($args[0]);
echo "运行开始******************************n".date('Y-m-d H:i:s');
foreach ($demos as $demo)
{
$this->getTest($demo);
}
echo "运行完成******************************n".date('Y-m-d H:i:s');

}

public function getTest()
{
//此处为www.111com.net你想要执行的代码,可以是php的任何代码包括SQL等
}

public function getDemo($index)
{
//此处为你想要执行的代码,可以是php的任何代码包括SQL等
return $index;
}
}

看到这里,不知道大家有没有什么疑问,有没有注意到run方法中的一个变量$args,而且在run方法下也有调用了$args[0],可以知道$args一定是一个数组。但是$args是从哪里来的呢?这个我们就需要追究一下php命令行的参数传递了,如:H:workspace>php test.php arg1 arg2
光看这个应该看不太懂吧,那就看看YII下执行command命令时候的参数吧,一下为Linux下的定时执行命令程序
0 15 1,11,21 * * cd /文件路径/&&/usr/local/php/bin/php yiic demo 0 >> /usr/home/test.log 【前面的数字代表,每个月的1、11、21号的下午3点执行命令】
在Yii的protected下执行命令方式:/usr/local/php/bin/php yiic create_excellenthall 0 注意到最后的0了吗?这个就是$args传递过来的,你可以换成数组中的任意一个键值。
描述的应该不是很专业,希望可以对有需要的人有帮助!

相关文章

精彩推荐