简体   繁体   English

Yii2 ::将控制器,模型和视图移动到子文件夹

[英]Yii2 :: Move controllers, models and views to subfolders

Have a structure: 有一个结构:

modules/
-- office/
---- controllers/
-------- industryController.php
-------- industryAttrController.php
-------- userAdminController.php
-------- userAdminAttrController.php
---- modules/
-------- industry.php
-------- industryAttr.php
-------- userAdmin.php
-------- userAdminAttr.php
---- views/
-------- industry/
-------- industry-attr/
-------- userAdmin/
-------- userAdminAttr/

I want to change it to: 我想将其更改为:

modules/
-- office/
---- controllers/
-------- industry/
------------ industryController.php
------------ industryAttrController.php
-------- user-admin/
------------ userAdminController.php
------------ userAdminAttrController.php
---- modules/
-------- industry/
------------ industry.php
------------ industryAttr.php
-------- user-admin/
------------ userAdmin.php
------------ userAdminAttr.php
---- views/
-------- industry/
------------ industry/
------------ industry-attr/
-------- userAdmin/
------------ userAdmin/
------------ userAdminAttr/

I try to move controllers in folders and I found in OfficeModule.php this code: 我尝试将控制器移动到文件夹中,并在OfficeModule.php中找到以下代码:

namespace app\modules\office;

/**
 * office module definition class
 */
class OfficeModule extends \yii\base\Module
{
    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'app\modules\office\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        $this->layout = 'office';
    }
}

But I can set only one folder for controllers here. 但是我在这里只能为控制器设置一个文件夹。 Can I add another paths or may it is impossible? 我可以添加其他路径还是可能?

Also I want to move models and views in subfolders too. 我也想在子文件夹中移动模型和视图。

I just started to learn Yii2, so please give me full answers. 我刚刚开始学习Yii2,所以请给我完整的答案。

Thank you. 谢谢。

There is some sort of "convention" here. 这里有某种“惯例”。 If you want to separate your logic on fronend / backend you should make them work like different modules. 如果要在前端/后端分离逻辑,则应使它们像不同的模块一样工作。 Your structure should look more like 您的结构应该看起来更像

backend
-assets
-config
-controllers
-models
-runtime
-views
-web
common
-config
-models
frontend
-assets
-config
-controllers
-models
-runtime
-views
-web

The main thing is that you should have components / controllers / models etc depending on the module not the path - so its path / all stuff not controller / path ... 最主要的是,您应该具有组件/控制器/模型等,具体取决于模块而不是路径-因此其路径/所有内容都不是控制器/路径...

In there you can put whatever folders you want, and just configure the namespaces correctly. 您可以在其中放置所需的任何文件夹,然后正确配置名称空间。 Such a projects is the Yii advanced template: 这样的项目是Yii高级模板:
https://github.com/yiisoft/yii2-app-advanced https://github.com/yiisoft/yii2-app-advanced

Look at it and you will probably understand the logic behind this separation. 看一下它,您可能会理解这种分离背后的逻辑。 If you have more questions I will answer you in the comments. 如果您还有其他问题,我会在评论中回答您。

Have read errors log and found solution: 阅读错误日志并找到解决方案:

1) Have change namespaces (controllers)
2) After that I had error that can't find view files, so I have move view/industry/ -> view/industry/industry
3) Have move models to subfolder
4) Have change all `use` in all files

But now I have a problem with path: site.local/office/industry/industry/index 但是现在我的路径有问题:site.local / office / industry / industry / index

I want to remove one industry from path. 我想从路径中删除一个行业。 How can I do that? 我怎样才能做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM