Ant100
Ant100

Reputation: 403

What's the correct architecture in MVC and codeigniter?

I know this type of question gets asked a lot, I myself have asked this question once, however no answer seems to fit my problem. I figure that by doing a 'general' aproach instead of a specific one it'll be more helpful for myself and others.

Questions like 'load a controller from a view' 'load a controller from a controller' 'load a view with controller from another view' get asked frequently, and what I have concluded so far is that it's not that there's no way to do it (I myself found a solution for this) it is that it's just a bad architecture issue.

So, my question is, what is a good architecture?

The template method is useful if you want to load different views and helps you organize. That's great. I think the problem is this:

-model1, controller1, view1

-model2, controller2, view2

-view3

-view4

You can find a way to load views 3 and 4 in either view 1 or 2. But what happens when you want to load view2 innside view1? There's no way to include controller2 in there.

I'm not asking for a way of including it, I'm asking what's the correct way of doing? I have seen the answer 'reuse the model' a lot. But I don't quite understand what that means.

I think answering this is not going to fix the problem I have so far (because the project is so advance i'd have to redoit), but it'll help me with future projects to do it right from start.

Thank you for your help.

Upvotes: 1

Views: 1015

Answers (2)

Ahmed Gaber
Ahmed Gaber

Reputation: 708

One of the best practice for views is to devise it for example three folders

  • common
  • content
  • layouts

Common : will have many html fragments like menues,sidebars, header, footer...etc and even some smaller parts like comment box or whatever

Content: is similar to common but more content related views that is useful in most of the websites beside common

Layout: this folder will contain the possible layouts for your website containing common and content views

this for the views for more understanding MVC check this article

Upvotes: 1

John Corry
John Corry

Reputation: 1577

I like to keep it simple...

Models interact with my database or other data source. That's all they do. They take data as input and return data.

Controllers act as request endpoints and control application flow/conditions. They get the right data (from the models), validate data (from users), make decisions about what to DO. Then, they load the right view(s) with the right data.

Views are simply HTML templates that DISPLAY whatever data the controller handed to them. The only 'processing' present in my views are things like iterating over arrays to produce repeatable elements, formatting and conditional display of elements/data.

The only real instance I can think of where one view loads another within it is for something like a navigation element or other elements that are common HTML repeated on multiple (but not ALL) pages.

Upvotes: 0

Related Questions