How to think of modules

How do you guys think of modules in yii?

I always thought of them as kind of "libraries" I could distribute and which others could integrate into their apps. And if I think of "libraries" which I include into one of my projects, I also think of using the classes that come with that "library", I think of aggregating and specializing them, using them the same way I use the classes that come with yii itself.

But now that I started using modules, it turned out, that this isn’t as straight forward as I thought it was. It might well be a problem with the design of my module or the classes within, but the more I think about it, the more yii’s modules look like “services” to me. I searched a lot through the forum, the guide and the wiki, on how to best use modules or components that come with them. The basic answer is something like this:




Yii::app()->getModule('moduleId')->foo();

Yii::app()->getModule('moduleId')->getComponent('componentId')->bar();



The same is true for controller actions that are invoked using an url route. The application knows which module to load, creates the controller and runs the action.

This is all about accessing an API of a black-box, which is basically how I think of “services”. You call methods on them, provide data as parameters, they somehow process that data and return a result. How they do the processing doesn’t matter, you don’t look inside.

So side by side:

"libraries" define a set of classes you can use.

"services" define an API you can use.

Accessing a module through its API is straight forward and seems to be the recommended way.

But how about using a class of a module from somewhere outside that module? My experience is, that doing it can work, but causes problems as soon as the classes rely on any configuration or initialization of their parent module.

So how do you use or see them? Do you think it’s best practice to distribute modules as “libraries” with the intention to access their classes? Or do you think they should better be used as services by only interacting with the API they provide?

I’d really appreciate your thoughts on this.

No. I don’t. :)

Modules are applications in an application.

Like a wiki module, for instance.

What you are talking about is components. Both regular components and application components.

Widgets also falls into this category.

Only use a module if you need a group of models/views/controllers/configuration - in other words: an app within an app - which does one task, like a wiki or a forum.

IMO. ;)

What I do is to first create a regular Yii application, then - when it works as intended - I convert it into a module and plunk it into other applications.

I do this because I want the modules to be independent of the application. But I guess it’s a matter of preference. :)

So you say modules are completed pieces of software? Apps that simply do their stuff. You use them through the controllers they provide (url routes, web services). And you don’t need them to interact with each other. I’d say this is the “service” point of view (I know the term isn’t quite correct), because to this point, you only use an existing API.

But didn’t you ever come to a situation, where you liked to reuse part of the logic from one of your modules outside that module? There are several posts in this forum where people asked how to do that. For example, there was somebody who converted the blog tutorial into a module and later liked to use a comment widget of that module somewhere else in his app.

Or how about behaviors? Imagine a module containing some of them. Wouldn’t it be nice to reuse them somewhere outside that module?

With this re-usability, we would move the point of view more towards "libraries", where you selectively access pieces of the whole thing and integrate them in your application.

best way is to think of modules as self containing application that can add additional features to your application without modifying what you have already done. I guess you can use them as libraries in some sense the fact that you can call method from module A for example in module B.

They are named ‘modules’ for a very good reason. :)

Read this:

guide/1.1/en/basics.module

A CWidget?

You can - and should - generalize that in a general widget which takes fields as parameters.

It doesn’t have to know what it’s used in. In fact, it’s good practice to make it totally context agnostic.

A behavior can be dropped into both our module and your application.

You can decide that your module depends on certain behaviors in your application, but I wouldn’t do it the other way around.

Yii is a collection of libraries, thus named a ‘framework’.

Once you have a collection of components, widgets, behaviors, etc. you essentially have a library.

IMO, you are walking down the wrong track when you view a module as a library. :)

It can be part of a library, but it’s not a library in itself.

It is, as the guide says, a self-contained software unit.