Application vs. Module vs. Controller folder

(Kalimero13cz) #1

I’ve used Yii1and Yii2 for several project of different sizes. Now it’s the first time I’m indevelopment of an app using Yii2 advanced template. I’d like to use the conceptof “apps” right, but I’m not confident even if I read the theory. So here are mythoughts:

What are the best real-life examples of using applications, modules and foldersin controller folder? What aspects should I consider when deciding what tochoose? Is it common design, login, url routing?

What should I choose for forum, shop, blog or API and why (actually all ofthese will be part of the website)? Why is forum used as an example (guide) fora module and admin/backend is an application? Isn’t forum the typicalapplication? On the other hand, why is blog used as an example (advancedtemplate docs) for application? Isn’t it more module or even just a part of themain websites with folder in controllers (or even without)?

Well I could go on. I really like the idea of separating the big project andfor example the API I can see as application (I used module in Yii1), but stillnot sure and absolutely unsure about the rest.

Instead of just saying “blog is this, forum is this”, I’d really appreciate thereasons why. Cause then I can understand it and choose wisely next time.

Thank guys for your work. I believe you’re making me a better programmer.

(Alexander Makarov) #2

Applications could be deployed to different servers or work as different webroots. Modules are parts of a single application which could be self-contained and configurable. Controllers are part of application and these typically are not self-contained.

All of these are separate applications with some integration. Makes sense to follow advanced project template structure.

Blog could be part of the main application if it’s very coupled (when posting user is given a rating, you can reuse other website objects in a blog etc.) but usually it doesn’t make sense.

(Kalimero13cz) #3

Could you please give me an example of a module, if all of these parts are supposed to be apps?

I as quite worried to call the blog or forum apps, because even the shop will in my case have the same design. Same layout and assets. For users, they will be the one thing. They won’t recognize differences. Sharing models and components is easy due the common folder, but different app was always a sign of different design to me.

But then I discovered, that set different layout to whole app or just controller is piece of cake. I struggled a bit with sharing assets, but then accomplished that by “abusing” the publish approach – I have one common Asset Bundle (which is registered in the common layout). Then I set the $sourcePath to @frontend/web/dist/, where my generated assets for web are (I generate them by Foundation 6). And therefore the $basePath is by default ‘@webroot’, it’s copied to the web/assets of every app (or even symlinked).

So is that the right approach how to deal witch common designs among the apps? Or does different app automatically mean separated design, like the backend has?

(Alexander Makarov) #4

Gii and Debug standard modules are good module examples. These are self-contained, configurable etc. A blog or a forum could be a module as well if it’s meant to be configurable and self-contained (not relying on anything in the application except if it’s stated by interfaces). It’s not easy to achieve though.

There’s another use of modules: grouping controllers within application. It’s not really what modules were designed for but that’s how these are often used. In this case modules are not self-contained and this could be applied in your case.

The approach you’ve taken is a bit better since you have better separation of different parts of the project. You may move some parts to separate servers to handle more users, you may split source code and give it to another team etc.

(Kalimero13cz) #5

Yep, debug module looks like a great example. It’s self-contained, accessible from the app and has some exact purpose in the app – it makes different results in different apps.

The Gii on the other hand – well, why module? How is it connected to the app? I see Gii rather as an app itself then a module. To me, it’s not related to the app. It generates files to any namespace – front, back, any. It has absolutely the same results no matter the app it’s registered in. My thoughts are, that it’s now registered into the app only to access it in url. Isn’t it just a burden from the simple template?

The module rule “self-contained” is truly strict and reserves modules for specific goals (which isn’t a bad thing). Can’t imagine develop forum as a module with this in mind. When you’re saying people use modules as some “let’s group some logic together” (forum), does it mean that the rule is “only” some convention and it’s ok to broke it? Because then I could imagine forum as a module easily.

I understand the advantage of app on separate server (even though the db is usually the bottleneck) and separated development, but how can I split the sourcecode? I mean if I split the apps into different repos, it won’t be deployable/runnable. It needs the common folder, vendor folder and the files from root, right? I guess duplicate the common parts to every repo isn’t good practice. What would you suggest? Have a separate repo for the common parts and then somehow manage to keep the common content compatible with other repos? That sounds silly. Or just have one repo and tell the devs “don’t you even think to look at another folder then ‘appforyou’, motherfu*ker!”? :)

Btw it has happened to me few times – the forum joins some random words from my post together. Strange.

(Alexander Makarov) #6

Yes, indeed Gii is more a separate app but there are important things to consider:

  1. It uses existing application config.

  2. It uses existing application files.

If made as a separate app, it would require user to configure all the paths and connection.

It can be done. I did that with wiki for Yii 1.1:

The “rule” isn’t really a rule. Nor it’s a convention. It’s the thought that was driving Yii core team creating modules concept. So yeah, modules could be used for both purposes or one of these: isolation and grouping.

Submodules or real separation. Of course, it should not be done while you don’t have the need for it because it would complicate things.

(Kalimero13cz) #7

Ok then. Thanks a lot for your answers. I’ll stick with the app approach yet and will see what struggles it’ll bring.

(Wadeshuler) #8

The ‘Project’ or ‘App Template’ is the entire site as a whole. In the Advanced app, this has multiple sections (or tiers): frontend, backend, console. Each is their own application because they run on their own and have their own config.

Say you wanted to add a blog, forum, or a wiki. You can either:

A) duplicate ‘frontend’ and name it ‘wiki’. Then fix the namespaces and config.

B) create a module inside ‘frontend’ named ‘wiki’.

Look at the Advanced Yii app. It has frontend, backend, and console. Modules work exactly like they do. A module has it’s own config, controllers, models, views, can have their own extensions, and modules can even have their own modules. A module is a self contained application. It’s like a mini-app.

Edited for clarity: I didn’t go to bed last night, so some areas went off track or didn’t explain what I really meant. I didn’t want to confuse anyone who stumbles here, so it has been refined and clarified.

(Kalimero13cz) #9

Hi Wade.

Thanks for your answer, but it has been already answered very well by samdark. Besides I suggest you reading these answers and documentation, because I think you’re not right in many things.

The “application” in fact is the frontend, backend, etc. There is no such a thing as “a section”. What you mean by an “application” is more like a “project”.

There is more misleading information. You’re describing MVC, which has never been an object of discussion, suggesting using an advanced project template (which I off course did – it wouldn’t have made any sense without it), calling frontend app “a module” or trying to compare a basic project template with a module with wrong conclusion.

The finale paragraph isn’t also the complete true. You can easily have more apps in your project (frontend, backend, api, blog) and have the same login. The apps can easily connect to the same database and share common models and other stuff in the“common” folder. This isn’t just a theory, this is already working like a charm.

Maybe someonewill correct me but I think my original question was based on information you are not right in.

(Wadeshuler) #10

You are correct, each are technically referred to as their own application:

What I was pointing out, is that “frontend” IS the exact same thing as a Module, only it is the top-most master parent Module. So when you create Modules, you are essentially creating another Yii app inside itself. The way they work, having their own controllers, models, views, config, etc. Modules can even have their own modules. When I first started with Yii2, I was turned away from Modules because I didn’t really get them. Then I realized, it was exactly the same and just a Yii app inside itself.

It is, in fact, a Module. It is called the Application because it is the top most one. When you break it down, at what makes up a module, and compare the structure of a module vs a yii application, they work exactly the same way. If you understand Yii, then you automatically understand how Modules work. A module is basically, an application inside an application.

Proof from the documentation, that what I am saying is correct:

Maybe that makes what I am trying to say clearer?

The only thing I was incorrect about, was saying:

It was the result of not going to bed last night, and I probably intended to write Project or Template. I don’t know where I was going with that? Sections was just used loosely. They are technically applications. The docs used them loosely too, calling them “tiers” ;)

The MVC reference was just to illustrate to anyone reading this thread, their role in all of this, and that Module is not the same as Model. You aren’t the only one who will read this, it may help someone else 5 months from now who stumble upon this thread.

(Kalimero13cz) #11

I’d say you’re definitely not right. A module is not an app. They behave differently. Technically, they are both completely different things. As I suggested – read samdark’s answers, because that was the point of my question – to show the differences between an app and a module and samdark did it – he showed some points where apps behave differently from modules. The fact that a module can have submodules doesn’t change anything. Actually it just proves, that if the apps and modules were the same, there wouldn’t be any need to have an app, because a module could do the same and contain another modules.

You have to bootstrap modules, you have to have Module.php, app has a lot more specifics, different folder structure (runtime, configs, web), etc, etc.

In my project I’m using 4 apps and two modules. Of course I can see that a module is in an app, but the fact, that both have mvc structure doesn’t mean they are the same.

But maybe we both are talking about a slightly different things and someone more clever could bright the problem.

(Gene Rodrigues) #12

Some thoughts…

Any MVC triad or set of MVC triads could be packaged as a module including the SiteController and its associated views. In the case of the Site module, you can define a defaultRoute in your main configuration for what you refer to as an ‘app’. It might be easier to think of your understanding of an ‘app’ as a ‘framework client’. The Yii framework can support any number of clients and all clients are dependent on the framework. MVC triads and modules are dependent on the client’s configuration.

Modules are more akin to applications relative to the Yii framework in the same sense as a standalone client application running on top of an operating system. Modules can be very simple, including a controller and views, or very complex containing dozens of MVC triads.

I build modules for reusability reasons. Once a module is created for a client, you can reuse it over and over again for other projects. For simple projects, this is just a matter of copying the module to the client directory structure and updating your configuration. For more complex projects, the concept of integrating MVC triads into a module is particulary useful if your project has an installer for installing and managing the various elements of a project including their dependencies and their configurations.

You can define the paths and base urls for all framework clients in an Advanced-like bootstrap file within your common configuration. The separate client configuration files determine the MVC triads and modules that can be launched by the respective client.

The common directory can include its own module directory or controller and views directories if those MVC triads are shared among multiple clients.

The Basic and Advanced templates should be thought of as examples. Using the ideas demonstrated in those templates and your own ideas, you can roll your own templates to meet your project needs and wants. Yii is unbelieveably versatile in this regard.

(Niizha Oh) #13

I’ll stick with the app approach yet.