How To Use Modules With Yii Advanced Application

I want to make complex modular application with Yii2 which will have frontend and admin section.

I consider to options:

  1. Advanced application template.

For example let say that I want to create comments module, and users should be able to post comments on posts, images, news, products and similar(each of those should be separate module).

Now I should create comment module in common directory, but how about frontend and backend application? Should I create same module in frontend/backend, just with different files(at least I will need to have controller and views for both applications). Is this the best approach approach, or there is better way?

  1. using custom application layout based on basic application layout(I know that I will need to modify many things here, adding separate config files, separate web directory, assets and so on), with module structure like this:

-modules

–controllers

—CommentAdminController.php

—CommentController.php

–models

—Comment.php

So, can you please suggest best way for this kind of application. I know Yii2 is still in beta, but this is long term project, without deadlines, so I am considering Yii2 only.

Not sure if this helps… the way I would attempt is if you can chart out a design and isolate the models, controllers, modules, components and identify these:

[list=1]

[*]Used in both frontend and backend: Create structure under common folder

[*]Used in frontend only: Create structure under frontend folder (maybe extending from the common classes)

[*]Used in backend only: Create structure under backend folder (maybe extending from the common classes)

[/list]

Well, almost all models will be shared by both frontend and backend application. The problem are controllers, views which would be different for each application, and in that case I don’t see how I could manage modules easy way(without having same module under common directory and frontend and backend application).

You exactly seem to have a structure. You can define a module with models, components and other common classes in common folder - that you can refer across.

As to controllers and views, you may want to explore:

[list=1]

[*]scenario 1: create them separately for frontend and backend and point your menu links and configure urlmanager accordingly. In your controller/views - you can refer common classes like common\modules\models\ClassName .

[*]scenario 2: you feel you have a lot of reusable Controller code that you can relegate within the module - then you may want to either extend a base controller from backend/frontend OR … just reconfigure module level Controller configuration to call a specific layout or view from backend or frontend based on user access rules.

[/list]

Well, that mean that I will need to create comments module in common, frontend and backend application?

What about structure #2 I suggested? Would something like that be possible or not and why?

IMO you may not have to necessarily recreate the module in all places. You can just place the module code in the common folder.

[list=1]

[*]If your module configuration is same for both frontend and backend - you can register your module in common/config/main.php. This should be available in both frontend and backend.

[*]If your module configuration is different for both frontend and backend - you can register your module separately in frontend/config/main.php and backend/config/main.php.

[/list]

You can access the module classes using proper namespaces in both frontend and backend (e.g. common\modules\Module or common\modules\models\Model).

Extending from a basic app - you would not have the separation of a frontend and backend (like separate apps). Since you mentioned you need a complex commenting module app. it may need separate UI & validations for admin and separate UI and validations for user frontend (for the same data set). If that’s the case, it maybe better IMHO if you use the advanced app.

Thank you for your reply, you are right, forms and model validation rules can be different for frontend/backend, it it would be mess to keep different rules/forms for backened/frontend :) That was key point :)

Great. Let know how you finally got to configure this in the end.