列出Cakephp 3中的所有控制器/操作 [英] List all controllers/actions in Cakephp 3

查看:332
本文介绍了列出Cakephp 3中的所有控制器/操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何列出我的网站上的所有控制器/操作? Configure :: listObjects('model')似乎不再存在。我试图写一个函数来生成/添加到我的ACL设置中的ACO。谢谢。

How do I list all the controllers/actions on my site? Configure::listObjects('model') doesnt seem to exist anymore. I am trying to write a function to generate/add to the ACO's in my ACL setup. Thanks.

推荐答案

所以这里是我做的。在我的资源控制器中:

So here is what I did. In my Resource Controller:

包含反射类/方法库

use ReflectionClass;
use ReflectionMethod;

要获取控制器:

public function getControllers() {
    $files = scandir('../src/Controller/');
    $results = [];
    $ignoreList = [
        '.', 
        '..', 
        'Component', 
        'AppController.php',
    ];
    foreach($files as $file){
        if(!in_array($file, $ignoreList)) {
            $controller = explode('.', $file)[0];
            array_push($results, str_replace('Controller', '', $controller));
        }            
    }
    return $results;
}

现在执行操作:

public function getActions($controllerName) {
    $className = 'App\\Controller\\'.$controllerName.'Controller';
    $class = new ReflectionClass($className);
    $actions = $class->getMethods(ReflectionMethod::IS_PUBLIC);
    $results = [$controllerName => []];
    $ignoreList = ['beforeFilter', 'afterFilter', 'initialize'];
    foreach($actions as $action){
        if($action->class == $className && !in_array($action->name, $ignoreList)){
            array_push($results[$controllerName], $action->name);
        }   
    }
    return $results;
}

最后,将两者绑定在一起:

Finally, to tie them boths together:

public function getResources(){
    $controllers = $this->getControllers();
    $resources = [];
    foreach($controllers as $controller){
        $actions = $this->getActions($controller);
        array_push($resources, $actions);
    }
    return $resources;
}

我希望能帮助一些人。

I hope that helps some people.

这篇关于列出Cakephp 3中的所有控制器/操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆