CodeIgniter Forums

Full Version: Switching from procedural to MVC?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Kibret[/eluser]
What are the steps to take when you change your codes from procedural to MVC (CodeIgniter)?

El Forum

[eluser]renownedmedia[/eluser]
Break it up into logical parts :p

Strip your database code from your logic code from your display code.

El Forum

[eluser]skunkbad[/eluser]
I think if you are asking, then you probably haven't read the docs. The whole concept of MVC really creates more organized code that is clean. Separated into parts, Models, Views, and Controllers will contain specific types of code. To understand what type of code belongs where, you might build a sample application using some of the examples in the docs, just to become more familiar with MVC. You might be thinking, as I did when I started, that the breaking up of all of your procedural code just makes things more complex, but, after you gain some experience using MVC, the benefits of your code being organized outweight the complexity created by splitting the code up. So, look at examples and take some time to learn.

El Forum

[eluser]BrianDHall[/eluser]
I was in the same boat, and created nearly an entire project 'the old way' (non-MVC) after hearing about the concept of MVC and it took that process to really 'get it'.

Basically, when you are using the older procedural/functional way you tend to end up with "spaghetti code". If you want to change the template and redesign the site, is that a simple change of a template file even someone who doesn't know much code can do...or would it be a multi-day chore for you just to implement the new template?

You've probabably heard of "separating content from formatting" where you try to define what you want to say in HTML but leave the "how it looks" to CSS? Or perhaps "sepparating business logic from content" in code.

MVC is the next step.

You have your Controller, with your business logic, like "if you are an administrator show the member management page, and handle the form if posted".

You have your Models, which are more like "Here's the membership info...now do something with it" - so they handle manipulation of your data and the creating/retrieving/updating/deleting (CRUD) of it as well. So a User model often may have a login() and logout() function, and update_account_info() function, etc etc. This is where you can use ORM to automate much of these functions for you.

Then there are Views, which is basically your html templates with perhaps a little bit of display logic. So for instance you have your account creation form View, and your Account Creation Success View, your Home Page View, etc.

This organization matters once you've gone past, say, 10+ pages with considerable logic involved. With proper use of Functions, and objects/classes once you get into some OOP, MVC is a tool that allows you to eliminate spaghetti code, allow clean and easy to maintain/refactor/update/expand code even when your application becomes very large.

Once you build a significant project using these methods, you see just what a difference it makes. You'll look back on the stuff you wrote before and think, "it would be easier to rewrite that then have to figure out what the hell is going on again...".

You can still use the system wrong, and CodeIgniter is very loose - it'll let you do whatever you want. In my first project I partially kept to the system of MVC, but violated in quite a few places, and now on my next project I'm keeping much more faithfully to the system.

It's one of those things that just Matter.

El Forum

[eluser]Kibret[/eluser]
I haven't been able to post my replies. But Thanks guys.

If there is some kind of a step-by-step guide (sort of hands-on tutorial) that would help. I am new to the whole MVC concept.

El Forum

[eluser]n0xie[/eluser]
[quote author="Kibret" date="1252291102"]I haven't been able to post my replies. But Thanks guys.

If there is some kind of a step-by-step guide (sort of hands-on tutorial) that would help. I am new to the whole MVC concept.[/quote]
Just follow the 10min blog intro and things should become more clear Smile