Home > php教程 > php手册 > ThinkPHP 3.2.2 获取项目所有方法名称

ThinkPHP 3.2.2 获取项目所有方法名称

WBOY
Release: 2016-06-07 11:41:29
Original
848 people have browsed it

ThinkPHP 3.2.2 获取项目所有方法名称,用途:呵呵!
如果使用了多级控制器,请自行修改代码。
    public function index(){<br>         $modules = array('Admin');  //模块名称<br>         $i = 0;<br>         foreach ($modules as $module) {<br>             $all_controller = $this->getController($module);<br>             foreach ($all_controller as $controller) {<br>                 $controller_name = $module.'/'.$controller;<br>                 $all_action = $this->getAction($controller_name);<br>                 foreach ($all_action as $action) {<br>                     $data[$i]['module'] = $module;<br>                     $data[$i]['controller'] = $controller;<br>                     $data[$i]['action'] = $action;<br>                     $i++;<br>                 }<br>             }<br>         }<br>         echo '<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">';&lt;br&gt;         print_r($data);&lt;br&gt;     }&lt;br&gt;     //获取所有控制器名称&lt;br&gt;     protected function getController($module){&lt;br&gt;         if(empty($module)) return null;&lt;br&gt;         $module_path = APP_PATH . '/' . $module . '/Controller/';  //控制器路径&lt;br&gt;         if(!is_dir($module_path)) return null;&lt;br&gt;         $module_path .= '/*.class.php';&lt;br&gt;         $ary_files = glob($module_path);&lt;br&gt;         foreach ($ary_files as $file) {&lt;br&gt;             if (is_dir($file)) {&lt;br&gt;                 continue;&lt;br&gt;             }else {&lt;br&gt;                 $files[] = basename($file, C('DEFAULT_C_LAYER').'.class.php');&lt;br&gt;             }&lt;br&gt;         }&lt;br&gt;         return $files;&lt;br&gt;     }&lt;br&gt;     //获取所有方法名称&lt;br&gt;     protected function getAction($controller){&lt;br&gt;         if(empty($controller)) return null;&lt;br&gt;         $con = A($controller);&lt;br&gt;         $functions = get_class_methods($con);&lt;br&gt;         //排除部分方法&lt;br&gt;         $inherents_functions = array('_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct', '_empty');&lt;br&gt;         foreach ($functions as $func){&lt;br&gt;             if(!in_array($func, $inherents_functions)){&lt;br&gt;                 $customer_functions[] = $func;&lt;br&gt;             }&lt;br&gt;         }&lt;br&gt;         return $customer_functions;&lt;br&gt;     }</pre><div class="contentsignin">Copy after login</div></div>改了下,用正则获取所有公共方法    public function index(){<br>         $modules = array('Admin');  //模块名称<br>         $i = 0;<br>         foreach ($modules as $module) {<br>             $all_controller = $this->getController($module);<br>             foreach ($all_controller as $controller) {<br>                 $controller_name = $controller;<br>                 $all_action = $this->getAction($module, $controller_name);<br>                 foreach ($all_action as $action) {<br>                     $data[$i] = array(<br>                         'name' => $controller . '_' . $action,<br>                         'status' => 1<br>                     );<br>                     $i++;<br>                 }<br>             }<br>         }<br>         echo '<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">';&lt;br&gt;         print_r($data);&lt;br&gt;     }&lt;br&gt; &lt;br&gt;     //获取所有控制器名称&lt;br&gt;     protected function getController($module){&lt;br&gt;         if(empty($module)) return null;&lt;br&gt;         $module_path = APP_PATH . '/' . $module . '/Controller/';  //控制器路径&lt;br&gt;         if(!is_dir($module_path)) return null;&lt;br&gt;         $module_path .= '/*.class.php';&lt;br&gt;         $ary_files = glob($module_path);&lt;br&gt;         foreach ($ary_files as $file) {&lt;br&gt;             if (is_dir($file)) {&lt;br&gt;                 continue;&lt;br&gt;             }else {&lt;br&gt;                 $files[] = basename($file, C('DEFAULT_C_LAYER').'.class.php');&lt;br&gt;             }&lt;br&gt;         }&lt;br&gt;         return $files;&lt;br&gt;     }&lt;br&gt; &lt;br&gt;     //获取所有方法名称&lt;br&gt;     protected function getAction($module, $controller){&lt;br&gt;         if(empty($controller)) return null;&lt;br&gt;         $content = file_get_contents(APP_PATH . '/'.$module.'/Controller/'.$controller.'Controller.class.php');&lt;br&gt;         preg_match_all(&quot;/.*?public.*?function(.*?)\(.*?\)/i&quot;, $content, $matches);&lt;br&gt;         $functions = $matches[1];&lt;br&gt;         //排除部分方法&lt;br&gt;         $inherents_functions = array('_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct','_empty');&lt;br&gt;         foreach ($functions as $func){&lt;br&gt;             $func = trim($func);&lt;br&gt;             if(!in_array($func, $inherents_functions)){&lt;br&gt;                 $customer_functions[] = $func;&lt;br&gt;             }&lt;br&gt;         }&lt;br&gt;         return $customer_functions;&lt;br&gt;     }</pre><div class="contentsignin">Copy after login</div></div>

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template