dongyi5570 2015-10-21 09:35
浏览 181
已采纳

获取所有方法

I'm attempting to build a role based access control in our PHP framework. The framework is on MVC architecture so every path works on /controller/action/param. We can get the controller and action on initialization and store them in variables, $controller, $action. Now my idea is to use a class to check the permissions of this action like:

Auth::permissions($controller, $action);

Now I'm hoping I could somehow create a script which would find all public methods of controllers inside a /modules/ folder. This way I could just run a script and it would update all controller actions as a list to the database, where we would get the role permissions from. This way I could avoid inserting all controller actions manually. Getting all the controllers is very easy as the folder structure is as:

/modules
    /controller
        controller.php

So I can just find all subdirectories on modules and add .php in the end. My question is that can I get the file's public methods somehow?

class Example extends Controller {
    public function main() {
        return 'foo';
    }
}

This way I could store this in the database as

example | main | role_id
  • 写回答

3条回答 默认 最新

  • dp13668681869 2015-10-21 09:43
    关注

    Here is a little code that can help you:

    <?php
    class Example {
        public function main() {
            return 'foo';
        }
        private function privatefunc(){
        }
        public function anotherpublicfunc(){
        }
    }
    
    
    $reflector = new ReflectionClass("Example");
    
    foreach($reflector->getMethods() as $method){
        if($method->isPublic()) {
            echo "Method ".$method->name." is public".PHP_EOL;
        }else{
            echo "Method ".$method->name." is not public".PHP_EOL;
        }
    }
    ?>
    

    output:

    Method main is public
    Method privatefunc is not public
    Method anotherpublicfunc is public
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能