When is it right to create a module for the application?

Hello Yii Developers,

I’ve got a simple question for you: Which criteria is to be taken into account when creating a module for the application?

There are lots of Yii tutorials that explain how to create and use a module, like this one: http://www.yiiframework.com/doc/guide/1.1/en/basics.module , but I still don’t understand why would I create a module if I can develop the application with regular models, views and controllers. Please pardon my ignorance but I’m a newbie.

Could you please let me know with an example of a simple application (real life) in which you would rather add a module to it.

Thanks in advance!

Alejandro.

Modules are not a replacement for "real applications". Rather they are applications that do one specific task. A good example would be the an access control module . Access control is something you will need in many of your projects. If you put it into a module you can use that module in multiple places instead of writing a new access control for each and every project.

Create a module if you want to implement reusable code,

means that parts of your current project can be used in other projects too.

So you can add features to a project by installing/configure modules

(usermanagement, mailing, interfaces …)

Extensions are often implemented as modules if they use components/controllers/models.

For code that is only useful for a specific project, generating a modul is not necessary,

but can be helpful if you want to switch on/off or configure some features in the config/main.php

Besides reusability as cited above

It depends how you want to structure your application

I personally use it a lot when developing medium / large applications

Lets say you are creating a CMS

I would put the public content outside modules and the management area in a backend module

Also another approach I take is to create a base controller for each module, that all controllers of that module extends this one

In this base controller I put the accessRules for that module there

Or if you want a forum that will require a lot of controllers/models, its better to separate it for better organization

Anyway, it all depends of structure and organization, I can’t see a scenario where you must use a module.

I think you will fall into one of 2 camps with modules:

  1. You need to develop an ‘app within an app’ and the functionality really is completely separate.

  2. You are going through your models folder and have to scroll 5 pages to find the one you need to update code in.

Modules are also one way to provide some organization in your overall app vs. making a subApp within. A CMS might have modules for backend and frontend functions although neither is technically an app unto itself.

I always use modules:

Lets say you start new app, and first thing you do is drop in a user management module:)

You want to add messaging, you add messaging module etc.

One thing to note is where to put extensions, to not duplicate it. I drop all into main app, so it unfortunatelly add dependency to module.

Thanks a lot for the reply. Got it now!

Alejandro.

Thank you for the response. That means that I could create an application by developing sets of modules and put in them together, which would allow me to reuse code and switch on/off modules according to requirements.

Is that right?

Alejandro.

Thank you Gustavo. I will take into account your comments.

Alejandro.

Hello there,

Thank you for replying. I think that in this case I would fit in option number 2, although I also need to create an app inside another app, but they are very similar and share functionality.

Alejandro.

Thank you mate!

Regarding the messaging module that you’ve mentioned, I thought Yii had a class to manage messages, but according to what you say it’d be better to create a complete module for it??

Alejandro.