Your SlideShare is downloading. ×
Thin controllers - fat models (Proper code structure for MVC)
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Thin controllers - fat models (Proper code structure for MVC)


Published on

How to write better code when using "MVC" framework? …

How to write better code when using "MVC" framework?

Remember to keep controllers "thin" and models "fat".

This presentation shows quite popular and bad practices when using MVC framework.
But it also tells how it should be done in order to have code easier to maintain.

Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Thin controllers - fat models How to write better code - MVC - Damian Sromek 2012-03
  • 2. The WRONG way - "mvC" WARNING: Do not try it at home/work!
  • 3. mvC - Controller● Whole business logic in a Controller.Controller:Ill do everything for you.You can keep everything in one place.Thats so simple!
  • 4. mvC - Model● Models used basically just to store data in database.Model:Im just a data so why should I do anything morethan just exist?
  • 5. mvC - View● View uses the database directly or via MysqliResult etc.View:Just give me database connection and Ill do everything!Give me more power and you wont need controllers andmodels at all!
  • 6. mvC - problems?● Copy/Paste to "reuse" the code.● Long and complex methods (actions) that need to be copied in order to use polymorphism.● Very difficult to test.
  • 7. You will pay for mvC
  • 8. The RIGHT way - MVC Makes your life easier
  • 9. MVC - Controller● Just a translator for a user wishes (request) so model and view can understand and respond in proper way (response).Controller:I should be so thin you should barely notice me.Ill just tell model what user did and the view to show whathe wants - facade for business logic.
  • 10. MVC - Model● This is where whole "magic" should happen.● Model should store and manipulate data.● It should be used intensively by controllers to do what user requested and by views to show data user wants to see.● Business logic should be in models, eg. "$ageLimit > 20".Model:Im the proper guy for doing the hard work.I know how your app should work and why.
  • 11. MVC - View● Gets a model or a collection of models selected by controller (according to user request) and shows it.● It tells the controller what user has done and what does he expect to happen.View:Ill show you what you want and let you do cool things withmodels.
  • 12. MVC - Action helper● Easy and good way to reuse code between controllers.● Thanks to models its using it keeps things DRY.● Prefer action helper over methods put in controllers extended by other controllers.Action helper:I can do common things with the models according to therequest. Im the controllers ... helper.
  • 13. MVC - View helper● Helps you render HTML template that you use on many views.View helper:I can help you make things (php, js and html code) looknice.
  • 14. OOP - Model● If you think youre using OOP because you have classes - you are wrong!● "Happy" object should have just one responsibility. "Fat model" does not mean it has to have hundred lines of code.● Object should be easy to test. You should be able to "mock" things its using.● Let object be dependent - inject into it the things it needs.
  • 15. MVC - JavaScript● All those rules applies to JavaScript and other languages.● JavaScript code is also great when you use MVC!So you should/could have:MV(JS: MVC)C
  • 16. Zend Framework uses MVC - really?● Its not a "true" MVC.● MVC was designed for desktop apps.● It has many "branches".@see MVP@see Model2 ("MVC for web apps")
  • 17. Questions?I will help you google the answer if you askdifficult question ;DThank You.Damian
  • 18. BibliographyImages found on the Internet and are not myproperty.- (Zend Framework Book: Surviving The Deep End)