Yii Framework Forum: Modules in Yii - Yii Framework Forum

Jump to content

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

Modules in Yii

#1 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,143
  • Joined: 17-January 09
  • Location:Russia

Posted 15 June 2016 - 05:07 AM

There are two problems Yii modules could solve:

1. Grouping alike functionality.
2. Isolating functionality for future reuse within different projects.

When Yii core team developed modules the purpose was always about both. When it went into the wild, community started using modules for grouping only more often than for isolation which is understandable since proper isolation is hard to achieve. If the module is isolated it never uses anything from outside of it. If it needs something, it provides an interface to be implemented by the one using the module. Same applies the other way around i.e. external usage of the module internals is strictly prohibited except what’s provided by module’s interfaces.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#2 User is offline   Yuv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 15-April 15

Posted 15 June 2016 - 08:09 AM

the way to go for modular approach is to always go right of the forward slash "/" always
//../../../
and never
../../../..//
right?

But i do have some question about the best approaches.
Assume i have 2 module:
- User
- Article

Both can live on their own. That is User does not have anything to do with Article and vice versa.

But now i want to couple these 2, i.e. implement some sort of liking and commenting system. What is the best way to do it?
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,143
  • Joined: 17-January 09
  • Location:Russia

Posted 15 June 2016 - 09:07 AM

I didn't get that about slashes...

About coupling of modules, use contract via interface instead. For example, in article module each post has author and we're displaying author name after the article. Since we don't want to couple to particular user implementation, we're introducing interfaces which are provided by article module:

interface ArticleInterace
{
    public function getAuthorName();

    // ...
}

inteface ArticleRepositoryInterface
{
    public function getById($id);
    public function getPage($page);
}


Then in the module itself we're using interfaces only and providing a way to set ArticleRepositoryInterface implementation in the module config. When module is to be used, developer has to implement these interfaces. It is typically simple, straightforward and, if interface is designed well, provides excellent flexibility.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#4 User is offline   Yuv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 15-April 15

Posted 17 June 2016 - 02:39 PM

well from the root of the module.. we can use all files that comes right of the root:
module_root/folder_x/file.y


and we shoul avoid going to any file which comes left of the root
file.a/folder_b/module_root



Because of these little needs, i tend to add coupling to some of my modules which results in a mess and confusion amond developers. Is this a repository design pattern?

I do get an idea about what you mentioned but i feel still kind of lost.

Designing the interfaces and implementing them seems easy enough but what do you mean about "Then in the module itself we're using interfaces only and providing a way to set ArticleRepositoryInterface implementation in the module config."

I think an example would really be helpful in this case.
0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,143
  • Joined: 17-January 09
  • Location:Russia

Posted 20 June 2016 - 04:04 AM

Ah, yes. Directly accessing files not within the module isn't good.

Quote

Because of these little needs, i tend to add coupling to some of my modules which results in a mess and confusion amond developers. Is this a repository design pattern?

No. The repository was just an example. That's about SOLID principles in general and proper encapsulation within the module.

Quote

Designing the interfaces and implementing them seems easy enough but what do you mean about "Then in the module itself we're using interfaces only and providing a way to set ArticleRepositoryInterface implementation in the module config."


Designing interfaces is hard. I'll write a cookbook tutorial about that but a bit later.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#6 User is offline   Yuv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 15-April 15

Posted 20 June 2016 - 09:57 AM

View Postsamdark, on 20 June 2016 - 04:04 AM, said:

Ah, yes. Directly accessing files not within the module isn't good.


No. The repository was just an example. That's about SOLID principles in general and proper encapsulation within the module.



Designing interfaces is hard. I'll write a cookbook tutorial about that but a bit later.


Ahh great, looking forward to it. Thanks
0

#7 User is offline   mmx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 20-June 11
  • Location:Virginia Beach, VA USA

Posted 02 June 2017 - 04:55 AM

View Postsamdark, on 15 June 2016 - 05:07 AM, said:

There are two problems Yii modules could solve:

1. Grouping alike functionality.
2. Isolating functionality for future reuse within different projects.

When Yii core team developed modules the purpose was always about both. When it went into the wild, community started using modules for grouping only more often than for isolation which is understandable since proper isolation is hard to achieve. If the module is isolated it never uses anything from outside of it. If it needs something, it provides an interface to be implemented by the one using the module. Same applies the other way around i.e. external usage of the module internals is strictly prohibited except what’s provided by module’s interfaces.

IAC (Interactive Application Communication). This requirement was solved in the original HMVC and PAC architectures using event/listener schemes. There should be plenty of docs on the web to outline the details as well as source code PHP source code on SourceForge. My mind might be regressing, but I think this was solved at least on the component level with the original PRADO framework. Ask Qiang Xue.

Say a module's config file listed mandatory dependencies and optional dependencies. Based on the configuration, a Article module would broadcast events and other modules could listen for those events. If the existance of a User module was an absolute requirement for initialization, the module would be in a position to fully initialize if the User module broadcast an acknowledgement event. If not, it would fail to initialize. If SEO, Comments, and Taxonomy modules were listed as optional dependencies in the config file, The Article module could use those services or any subset of those services if they broadcast an acknowledgement event. This pretty much applies to any service-like module using pivot tables.

One could probably mimic the same process using RPCs or SOAP or another IAC protocol. Not sure, but I think someone tried to do this for 1.1.
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