Skip to content

Commit

Permalink
thinkphp3.2 cli模式的正确使用方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Oct 30, 2016
1 parent 7e90b11 commit 10ee91b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 10 deletions.
43 changes: 43 additions & 0 deletions Backend/Home/Common/function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
header("Content-type:text/html;charset=utf-8");

/**
* =====================================================================================================================
* 传递数据以易于阅读的样式格式化后输出
* @param $data
* =====================================================================================================================
*/
function homePrint($data)
{
// 定义样式
$str = '<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
// 如果是boolean或者null直接显示文字;否则print
if (is_bool($data)) {
$show_data = $data ? 'true' : 'false';
} elseif (is_null($data)) {
$show_data = 'null';
} else {
$show_data = print_r($data, true);
}
$str .= $show_data;
$str .= '</pre>';
echo $str;
}

/**
* php cli模式执行php文件
* 测试
*/
function cli_test()
{
$count = 0;
while(true){
$count++;
echo $count."\r\n";
if($count > 10){
break;
}
sleep(3);
}
echo 'done';
}
28 changes: 24 additions & 4 deletions Backend/Home/Controller/DataBaseController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,32 @@ public function createRedis()
return $rPushResul;
}

public function executeFunction()
/**
* php cli模式执行php文件
* 模拟队列发送邮件
*/
public function executeCli()
{
$dir = 'D:\wamp\bin\php\php5.5.12>php.exe';
exec("D:\wamp\bin\php\php5.5.12>php.exe ../cli_test.php");
}

/**
* php cli模式执行php文件
* D:\wamp\bin\php\php5.5.12>php.exe ../cli_test.php
*/
public function cli_test()
{
for ($x=0; $x<=10000; $x++)
{
$this->createRedis();
$count = 0;
while(true){
$count++;
file_put_contents("./test_result.txt",$count."\r\n",FILE_APPEND);
if($count > 10){
break;
}
sleep(3);
}
echo 'done';
}


Expand Down
9 changes: 3 additions & 6 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@
* new:http://serverName/home.php/index
*/
//define('BIND_CONTROLLER','Index');
//php-cli http://www.thinkphp.cn/topic/34761.html

// 定义应用目录
define('APP_PATH','./Backend/');
define('APP_PATH',dirname(__FILE__).'/Backend/');

// 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';



// 亲^_^ 后面不需要任何代码了 就是如此简单
require dirname( __FILE__).'/ThinkPHP/ThinkPHP.php';
47 changes: 47 additions & 0 deletions admin_bak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@



<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------

// 应用入口文件

// 检测PHP环境
if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !');

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG',True);

/**
* 绑定Home模块到当前入口文件(3.2.1以上版本写法)
* old:http://serverName/index.php/Home/Index/index
* new:http://serverName/home.php/Index/index
*/
//define('BIND_MODULE','Home');


/**
* 绑定Index控制器到当前入口文件(3.2.1以上版本写法)
* old:http://serverName/index.php/Home/Index/index
* new:http://serverName/home.php/index
*/
//define('BIND_CONTROLLER','Index');

// 定义应用目录
define('APP_PATH','./Backend/');

// 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';



// 亲^_^ 后面不需要任何代码了 就是如此简单

0 comments on commit 10ee91b

Please sign in to comment.