搜索
系统检测到您的用户名不符合规范:

thinphp5一键注册所有Auth规则

浏览:1464 发布日期:2016年11月22日 分类:ThinkPHP5专区 关键字: thinkphp5 Auth认证 注册规则 注册权限 注册rule
代码中的方法的风格一定要写成如下图所示的形式
class Auto extends Base
{
/**
* 注册
*/
public function register(){

$modules = array('manager'); //模块名称
$i = 0;
foreach ($modules as $module) {
$all_controller = $this->getController($module);
foreach ($all_controller as $controller) {
$controller_name = $controller;
$all_action = $this->getAction($module, $controller_name);
foreach ($all_action as $action) {
$tmp=strtolower(substr($controller, 0,-4));
$data[$i] = array(
'name' =>$tmp . '/' . strtolower($action),
'status' => 1,
'desc'=>$this->get_cc_desc($module,$controller,$action)
);
$i++;
}
}
}
$obj = new AuthRule();
$msg = '';
foreach ($data as $k => $v) {
if (!$obj->where(array('name' => $v['name']))->find()) {
if ($obj->insert(array('name' => $v['name'],'title'=>$v['desc']))) {
$msg = $msg . $v['name'] . '注册成功<br/>';
} else {
$msg = $msg . $v['name'] . '注册失败<br/>';
}
} else {
$obj->where(array("name"=>$v['name']))->update(array("title"=>$v['desc']));
$msg = $msg . $v['name'] . '已注册<br/>';
}
}
$this->assign("msg",$msg);
return $this->fetch('System/register');
}

/**
* @cc 获取所有控制器名称
*
* @param $module
*
* @return array|null
*/
protected function getController($module){
if(empty($module)) return null;
$module_path = APP_PATH . '/' . $module . '/controller/'; //控制器路径
if(!is_dir($module_path)) return null;
$module_path .= '/*.php';
$ary_files = glob($module_path);
foreach ($ary_files as $file) {
if (is_dir($file)) {
continue;
}else {
$files[] = basename($file, config('DEFAULT_C_LAYER').'.class.php');
}
}
return $files;
}




/**
* @cc 获取所有方法名称
*
* @param $module
* @param $controller
*
* @return array|null
*/
protected function getAction($module, $controller){
if(empty($controller)) return null;
$content = file_get_contents(APP_PATH . '/'.$module.'/controller/'.$controller);

preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
$functions = $matches[1];

//排除部分方法
$inherents_functions = array('__construct','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct','_empty');
foreach ($functions as $func){
$func = trim($func);
if(!in_array($func, $inherents_functions)){
if (strlen($func)>0) $customer_functions[] = $func;
}
}
return $customer_functions;
}


/**
* @cc 获取函数的注释
*
* @param $module Home
* @param $controller Auth
* @param $action index
*
* @return string 注释
*
*/
protected function get_cc_desc($module,$controller,$action){
$desc='app\\'.$module.'\controller\\'.$controller;
$desc=substr($desc, 0,-4);
$func = new \ReflectionMethod(new $desc(),$action);
$tmp = $func->getDocComment();
$flag = preg_match_all('/@cc(.*?)\n/',$tmp,$tmp);
$tmp = trim($tmp[1][0]);
$tmp = $tmp !='' ? $tmp:'无';
return $tmp;
}
}
最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
suhaoxiang
积分:3367 等级:LV4
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /