Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules模块下的模型怎么样才可以自动加载? #559

Open
xrayffa opened this issue Oct 4, 2021 · 1 comment
Open

modules模块下的模型怎么样才可以自动加载? #559

xrayffa opened this issue Oct 4, 2021 · 1 comment

Comments

@xrayffa
Copy link

xrayffa commented Oct 4, 2021

我在modules目录下建立了若干个子模型文件夹和文件,其路径和文件内容为:

modules\sys\models\My.php
class MyModel { static function info(){ return 123456; } }

然后我在别的控制器尝试调用这个模型:
public function ccAction() { $data = MyModel::info(); echo $data; }

发现是报错的:
Fatal error: Uncaught Error: Class 'MyModel' not found in....

所以我提问:怎么样访问这种自定义模块\模型里面的方法?

(其实使用import的方式导入这个模型是可以访问到里面的方法的,但是模型既然创建出来,肯定是要访问的,框架为何不直接自动加载?)
ps:其实模块下的控制器是自动加载的(modules\模块\controllers内的控制器),那就想不通模型(modules\模块\models内的模型)为什么不会自动加载?

===================
另外,控制器的类名貌似不支持驼峰写法呀?不管写的是什么一律转换成小写了
例如
UserInfo.php 最终被转成 Userinfo.php来请求导致文件找不到... 不科学不科学

@Sgenmi
Copy link

Sgenmi commented Feb 22, 2022

public function _initRegisterNameSpace(){
        //注册模块名命名空间
        $loader = \Yaf\Loader::getInstance(APP_PATH.'/library');
        $modules= \Yaf\Registry::get('_modules');
        if($modules && is_array($modules)){
            foreach ($modules as $v){
                $loader->registerNamespace(sprintf("\%s\Model",$v), APP_PATH."/modules/{$v}/models");
                $loader->registerNamespace(sprintf("\%s\Service",$v), APP_PATH."/modules/{$v}/services");
                $loader->registerNamespace(sprintf("\%s\Command",$v), APP_PATH."/modules/{$v}/command");
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants