MrD
MrD

Reputation: 2481

How to properly use Yii modules?

In Yii PHP framework, one has ability to create modules. As per Yii's official documentation here is definition of the module:

A module is a self-contained software unit that consists of models, views, controllers and other supporting components. In many aspects, a module is similar 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.

Let's say we have a huge aplication and we have to create front-end and backend. In this case, is it better to create frontend module, and backend module and use them, or it is better to implment frontend as one Yii application, and backend as second Yii application.

I'm asking this because if you look at Yii's 2 advance template, there three are different applications (common, backend, frontend), but they are not implemented as three different modules, and my question is why?

Is app is going to be slower when you use modules and what are pros and cons of using modules?

Upvotes: 2

Views: 792

Answers (2)

Mihai P.
Mihai P.

Reputation: 9367

Yii2 advance template has 3 different applications however they are frontend, backend and console (not common).

is it better to create frontend module, and backend module and use them?

The answer is "it depends". Lets take some examples:

  1. I have a "users" section in the backend that tells the system what can each user change in the backend. I use this in multiple applications with no change at all so I have created a user module that I can just insert wherever I want. It is the simplest module because I never use it for the frontend.
  2. a blog module, the blog module is a little more complicated as it has a place to manage the blog (this part is in the backend) and the posts and comments shown in the front section (this part should be in the frontend). However I still want to be able to plug it in multiple applications. My solution was to create some folders in the actual module (I actually created the same structure frontend / backend / common). The logic is the same, in my website frontend I use what you find in the frontend folder of the module, the common holds the things that I use in both the frontend and backend (like some models) etc. Different application will use the same frontend controllers / widgets but make sure you allow the views to be changed.

The answer to use or not to use modules is actually the same answer to "Will I use this in other applications and can it function without the rest of the application?" A module should NOT be tight coupled with the rest of the application.

Upvotes: 3

SITIO
SITIO

Reputation: 41

The idea of the modules in that they almost autonomous. At the stage of application development you must highlight from it all stand-alone elements. For example, I have a large CRM. I need a user module (includes all models associated with the user, controllers, display components and their configuration - for example the rules of routes). There is also an administrative module. There CRM module and a module for corporate events and meetings schedules. And about 3-5 independent modules - for example internal communications module,access control module and module of electronic payments. The convenience is that the all modules are self-contained. They have their own MVC- sets, sets of widgets and configurations. And they can be moved from one project to another without much effort.

Here's another interesting modular system: There are resource with various functionality. It can be divided into modules. Each module file should implement a modular interface (you need to write it). For example, I want each module to provide its own widget for menu.(Drop-down list or just a button) and its own widget for dashboard. Also in the module file specified access to various actions, etc.

There is a basic component,that prior to the step of rendering polls all modules at their regulated functionality and builds on its basis the menu, dashboard and other widgets. The main thing with this approach, keep in mind, that everything that module provides must be prepared by the modular functionality(models, widgets) not to disrupt the modular autonomy.

Upvotes: 0

Related Questions