cakephp - scandir无法打开dir错误PHP:scandir

问题描述 投票:1回答:1

我正在尝试获取控制器文件列表

$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

php cakephp cakephp-3.0 scandir
1个回答
1
投票

Only use PHP: scandir with a full path.

虽然PHP文档没有说明它,但scandir通常不会对相对路径起到很好的作用。 (虽然有些情况下相对路径可以完美地工作。)

Path arg is a relative path here

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

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

Solution 1: Use CakePhp: constant APP to build the full path

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

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

Solution 2: Use PHP: $_SERVER['DOCUMENT_ROOT'] to build a full path.

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

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

© www.soinside.com 2019 - 2024. All rights reserved.