cakephp - scandir无法打开dir错误

时间:2017-12-27 20:20:27

标签: php cakephp cakephp-3.0 scandir

我试着获取控制器文件列表

$files = scandir(APP_DIR . '/Controller/');

...但是我收到了这个错误:

Warning (2): scandir(src/Controller/): failed to open dir: No such file or directory [APP/Controller/HomeController.php, line 13]

...当我尝试从列表中删除一些文件名时: $files = array_diff( $files, array('.','..','Component','AppController.php','HomeController.php'));

...我收到此错误:

Warning (2): array_diff() [function.array-diff]: Argument #1 is not an array [APP/Controller/HomeController.php, line 23]

我的全班同学看来:

<?php
namespace App\Controller;
use ReflectionClass;
use ReflectionMethod;

class HomeController extends AppController{
    public function index(){

        /**
         * http://stackoverflow.com/questions/20963786/how-do-i-get-a-list-of-all-functions-inside-a-controller-in-cakephp
         * http://stackoverflow.com/questions/25892594/list-all-controllers-actions-in-cakephp-3
         */

        $files = scandir(APP_DIR . '/Controller/');
        $controllers = [];
        $ignoreFilesList = [
            '.',
            '..',
            'Component',
            'AppController.php',
            'HomeController.php'
        ];

        $files = array_diff( $files, array('.','..','Component','AppController.php','HomeController.php'));
        $className = '';
        $class = null;
        $ignoreList2 = ['beforeFilter', 'afterFilter', 'initialize', 'delete'];


        echo is_array ( (array)$files );// returns 0 without cast

        foreach( (array)$files as $file){

                $controller = str_replace('Controller', '', explode('.', $file)[0]);
                array_push($controllers, $controller); //str_replace('Controller', '', $controller));

                //dump( $controller );

                /* controller methods */
                $this->$className = 'App\\Controller\\'.$controller.'Controller';
                $this->$class = new ReflectionClass($this->$className);
                $methods = $this->$class->getMethods(ReflectionMethod::IS_PUBLIC);

                echo "<ul><li>$controller</li>";
                echo "<ol>";

                foreach($methods as $method){

                    if ( isset( $method->name )){

                        if($method->class == $className && !in_array($method->name, $ignoreList2))
                        {
                            $controllers[$controller][]= $method->name;
                            echo "<li>$method->name</li>";
                        }
                    }
                }
                echo "</ol>";
                echo "</ul>";

        }

        $this->set('allControllers', $controllers );
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

仅使用PHP: scandir的完整路径。

虽然PHP文档没有说明任何内容,但 scandir 通常对相对路径不起作用。 (虽然有些情况下相对路径可以很好地工作。)

路径arg是这里的相对路径

由于APP_DIR存储单个目录名而不是路径,因此scandir将尝试访问src/Controller/,这可能与当前正在运行的脚本无关。

$files = scandir(APP_DIR . '/Controller/');

解决方案1:使用CakePhp: constant APP构建完整路径

感谢ndm在他对OP的评论中提醒我们APP常数。 APP存储应用程序目录的绝对路径,包括尾部斜杠。

$path = APP .'Controller/';
$files = scandir($path);

解决方案2:使用PHP: $_SERVER['DOCUMENT_ROOT']构建完整路径。

$path = $_SERVER['DOCUMENT_ROOT'] . '/'. APP_DIR .'/Controller/';
$files = scandir($path);

或者, 可以 能够使用PHP: Realpath来检索完整路径。 *提醒:工作目录会因运行的脚本而异。