自己写的比较简单的thinphp代码生成控制器
基于thinkphp3,方便自己使用。通过链接创建
例如访问:localhost/index.php/Home/Cqh/n/auto
提示: Auto控制器创建成功
Auto模型创建成功
Auto视图创建成功
弄着玩的,可以基于自己的需求进行改动。
<?php
namespace Home\Controller;
use Think\Controller;
class CqhController extends Controller
{
/**
* @Author JY
* @copyright Cqh代码生成
* $m 指定模块目录,默认为Home
* $n 指定控制器与模型的名字
* $v 指定生成视图文件默认生成index.html
* 若为数字则不生成
*/
public function index()
{
$m = ucwords(I('get.m')?:"Home");
$n = ucwords(I('get.n'));
$v = I('get.v');
if($n){
$controller = $this->template($m,$n);
$model = $this->template($m,$n,1);
}
if(!is_numeric($v)){
$v = $v?:"index";
$url = APP_PATH."/Home/View/$n/";
mkdir($url);
$view = file_put_contents($url."{$v}.html","");
}
echo $controller?"{$n}控制器创建成功<br/>":"{$n}控制器创建失败<br/>";
echo $model?"{$n}模型创建成功<br/>":"{$n}模型创建失败<br/>";
echo $view===0?"{$n}视图创建成功<br/>":"{$n}视图创建失败<br/>";
}
public function template($m,$n,$mode=FALSE){
$tpl = $mode?'Model':'Controller';
$res = "<?php \n";
$res .= "namespace $m\\$tpl; \n";
$res .= "use Think\\$tpl; \n";
$res .= "class $n$tpl extends $tpl{ \n";
$res .= " public function index(){} \n";
$res .= "}";
$url = APP_PATH."/Home/$tpl/";
return file_put_contents($url."{$n}Controller.class.php", $res);
}
}