Получить весь маршрут (модуль, контроллер, действие) в приложении Yii2 - PullRequest
1 голос
/ 23 марта 2020

Я хочу получить весь маршрут из моего приложения в Yii2 Advanced. я закончил sh эту проблему, но я хочу, чтобы она соответствовала каталогу.

мой код для внешнего интерфейса / модулей и все, что находится внутри:

    $dir = Yii::getAlias('@frontend');
$getFile = \yii\helpers\FileHelper::findFiles($dir,['only'=>['*.php']]);
$files['controllers'] = [];
$files['modules'] = [];
foreach($getFile as $file){
    if (substr($file, strrpos($file, '.') - 6) == 'Module.php') {
        $mname = strtolower(str_replace('Module','',basename($file,".php")));
        $mfilename = basename($file,".php").'.php';
        $mpath = str_replace($mfilename,'',$file);
        $mroute = '/'.str_replace('Module','',$mname);
        $files['modules'][$mname]['path'] = $mpath;
        $files['modules'][$mname]['filename'] = $mfilename;
        $files['modules'][$mname]['route'] = strtolower($mroute);
        $getFileCont = \yii\helpers\FileHelper::findFiles($mpath,['only'=>['*.php']]);
        foreach($getFileCont as $file){
            if (substr($file, strrpos($file, '.') - 10) == 'Controller.php') {
                $cname = strtolower(str_replace('Controller','',basename($file,".php")));
                $cfilename = basename($file, ".php").'.php';
                $cpath = str_replace($cfilename,'',$file);
                $croute = $mroute.'/'.str_replace('Controller','',$cname);
                $files['modules'][$mname]['controllers'][$cname]['path'] = $cpath;
                $files['modules'][$mname]['controllers'][$cname]['filename'] = $cfilename;
                $files['modules'][$mname]['controllers'][$cname]['route'] = strtolower($croute);
                $content = file_get_contents($file);
                preg_match_all('#function action(.*)\(#ui', $content, $aData);
                $files['modules'][$mname]['controllers'][$cname]['actions'] = [];
                foreach($aData[1] as $action){
                    $act = strtolower($action);
                    // $files['modules'][$mname]['controllers'][$cname]['actions'][$act]['name'] = $action;
                    // $files['modules'][$mname]['controllers'][$cname]['actions'][$act]['function'] = 'function'.$action;
                    $files['modules'][$mname]['controllers'][$cname]['actions'][$act]['route'] = strtolower($croute.'/'.$action);
                }
            }
        }
    }
}

это код для внешнего интерфейса / siteController:

$getFileController = \yii\helpers\FileHelper::findFiles($dir.'/controllers',['only'=>['*.php']]);
foreach($getFileController as $file){
    if (substr($file, strrpos($file, '.') - 10) == 'Controller.php') {
        $cname = strtolower(str_replace('Controller','',basename($file,".php")));
        $cfilename = basename($file, ".php").'.php';
        $cpath = str_replace($cfilename,'',$file);
        $croute = '/'.str_replace('Controller','',$cname);
        $files['controllers'][$cname]['path'] = $cpath;
        $files['controllers'][$cname]['filename'] = $cfilename;
        $files['controllers'][$cname]['route'] = strtolower($croute);
        $content = file_get_contents($file);
        preg_match_all('#function action(.*)\(#ui', $content, $aData);
        $files['controllers'][$cname]['actions'] = [];
        foreach($aData[1] as $action){
            if($action !== 's') {
                $act = strtolower($action);
                $files['controllers'][$cname]['actions'][$act]['route'] = strtolower($croute.'/'.$action);
            }
        }
    }
}
print_r($files);

выходное значение:

Array
(
    [controllers] => Array
        (
            [site] => Array
                (
                    [path] => /var/www/html/aoisora/_protected/src/frontend/controllers/
                    [filename] => SiteController.php
                    [route] => /site
                    [actions] => Array
                        (
                            [index] => Array
                                (
                                    [route] => /site/index
                                )

                            [login] => Array
                                (
                                    [route] => /site/login
                                )

                            [logout] => Array
                                (
                                    [route] => /site/logout
                                )

                            [contact] => Array
                                (
                                    [route] => /site/contact
                                )

                            [about] => Array
                                (
                                    [route] => /site/about
                                )

                            [signup] => Array
                                (
                                    [route] => /site/signup
                                )

                            [requestpasswordreset] => Array
                                (
                                    [route] => /site/requestpasswordreset
                                )

                            [resetpassword] => Array
                                (
                                    [route] => /site/resetpassword
                                )

                            [verifyemail] => Array
                                (
                                    [route] => /site/verifyemail
                                )

                            [resendverificationemail] => Array
                                (
                                    [route] => /site/resendverificationemail
                                )

                        )

                )

        )

    [modules] => Array
        (

            [post] => Array
                (
                    [path] => /var/www/html/aoisora/_protected/src/frontend/modules/blog/modules/post/
                    [filename] => PostModule.php
                    [route] => /post
                    [controllers] => Array
                        (
                            [default] => Array
                                (
                                    [path] => /var/www/html/aoisora/_protected/src/frontend/modules/blog/modules/post/controllers/
                                    [filename] => DefaultController.php
                                    [route] => /post/default
                                    [actions] => Array
                                        (
                                            [index] => Array
                                                (
                                                    [route] => /post/default/index
                                                )

                                        )

                                )

                        )

                )

            [blog] => Array
                (
                    [path] => /var/www/html/aoisora/_protected/src/frontend/modules/blog/
                    [filename] => BlogModule.php
                    [route] => /blog
                    [controllers] => Array
                        (
                            [default] => Array
                                (
                                    [path] => /var/www/html/aoisora/_protected/src/frontend/modules/blog/controllers/
                                    [filename] => DefaultController.php
                                    [route] => /blog/default
                                    [actions] => Array
                                        (
                                            [index] => Array
                                                (
                                                    [route] => /blog/default/index
                                                )

                                        )

                                )

                        )

                )

        )

)

проблема: [route] => / post / default / index должно быть [route] => / blog / post / default / index , поскольку запись доступна в модулях блога. я использую метод с помощником файла, каждый файл с именем ..Module. php или ..Controller. php это может быть возвращаемое значение.

...