简体   繁体   中英

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:

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.

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:
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

I want to remove one industry from path. How can I do that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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