Yii Framework Forum: How to think of modules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to think of modules Rate Topic: -----

#1 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 15-March 09

Posted 24 June 2011 - 01:32 PM

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:

Quote

You need to know which module to use, and if you know it, use it 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.
Don't like ads in my sig...
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 June 2011 - 02:18 PM

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. ;)
"Less noise - more signal"
1

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 June 2011 - 02:25 PM

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. :)
"Less noise - more signal"
0

#4 User is offline   Ben 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 15-March 09

Posted 25 June 2011 - 06:14 AM

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.
Don't like ads in my sig...
0

#5 User is offline   Soran 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 30-October 10

Posted 27 June 2011 - 09:30 AM

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.
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 28 June 2011 - 07:25 AM

View PostBen, on 25 June 2011 - 06:14 AM, said:

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.

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

Read this:
guide/1.1/en/basics.module

Quote

A module is a self-contained software unit that consists of models, views, controllers and other supporting components.
In many aspects, a module resembles to an application.

The main difference is that a module cannot be deployed alone and it must reside inside of an application.
Users can access the controllers in a module like they do with normal application controllers.

Modules are useful in several scenarios. For a large-scale application, we may divide it into several modules, each being developed and maintained separately. Some commonly used features, such as user management, comment management, may be developed in terms of modules so that they can be reused easily in future projects.


View PostBen, on 25 June 2011 - 06:14 AM, said:

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.

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.

View PostBen, on 25 June 2011 - 06:14 AM, said:

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

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.

View PostBen, on 25 June 2011 - 06:14 AM, said:

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.

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.
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users