48JIGEN *Reloaded*

Re: Best Practices in MVC Design with CakePHP



This is for the 4th day in CakePHP Advent Calendar 2010.

The 3rd day's entry "「Modelとの付き合い方(How best you can handle models)」", posted by shin1x1, looks pretty useful to me. Whenever modifying source code, ever since I started to use CakePHP, I have spent much time to settle this issue: what kind of logic better to be put in models, and how best I can do it? shin1x1's suggestions could be a good help to me, In particular, the 3rd and 4th items in his post.

Today, I'm gonna introduce the best article I have ever read in MVC Design, which tells you the importance of models too. However, unlikely shin1x1's post, my post will be helpful for who is fresh or is being at start-up stage in CakePHP or MVC Design, rather than experts.

Best Practices in MVC Design with CakePHP


The article is "Best Practices in MVC Design with CakePHP", written in March 2008 by @nateabele who is Lead Developer of Lithium and was invited to give a talk in PHPMatsuri 2010 which I joined as well. The guy standing at right most is @nateabele.(pic is from PHPMatsuri, is taken by lllnoriko4lll. Thanks!)

What's notable is that, fine practices are provided by tutorial as a study material.. the original article is not available currently(as long as I explored google 12/03/2010), however good thing you can try archived one by archive.org or Japanease translated version article. (Big thank you to both @nateabele and @junya who translated) :D

Take note the article contains fine practices in MVC Design, which can be applied to any other MVC framework. If you are interested in MVC Design and not familiar with that as of now, I recommend you to try it out even if you are not good at PHP.

Models, Models, Models

The tutorial teaches you how much models are important in MVC Design. Take a look at following passages for example:

Doesn't it sound fabulous?

Move More Logic To Models If You Can

Here is a passage I feel the most important in the article:

When deciding what logic to put where, here’s a good rule of thumb: anything that can go in the model, should

Honestly, until I came across this rule, total volume of codes in controllers had been much(3 times or more) bigger than the one in models. I knew this balance is bad, there is definitely rules as they should follow, but I didn't have enough time to do so, so just left them over for a long time. A good news to me was not only the happening to meet with this article, but also the fact that text volume in the article is as short as I could read quickly.

For those who have used framework without understanding the significance of models beforehand(like me), or for those who have studied them already but have never been confident enough with practical usage of framework, this article is a must read. As it introduces how to edit source code step by step, you can try and learn them as if you are privately tutored by @nateabele, and it will further deepen your understanding in MVC Design(and CakePHP as well).

Get Connected by CakePHP Advent Calendar 2010

As you get better in MVC Design, and as you move more code from controllers, then another issue will be emerged : total size of codes in models will increase instead. For this issue, "4. アクション毎にModelを作る(Prepare Models To Each Action)" in yesterday's post by shin1x1 might be a possible solution for this. Other solutions will do of course.

It is a big fun to deepen your understanding of CakePHP and get those knowledges connected through CakePHP Advent Calendar 2010. Certainly more things will happen on this December, I can't wait them!

On the 5th day, tomorrow is @uechoco's turn. Stay tune to CakePHP Advent Calendar!

( * English version)

CakePHP Advent Calendar 2010の4日目です。





既にご存知の方が多いと思うのですが、紹介するのは「CakePHPを使ったMVC設計のベストプラクティスhatenaという記事で、PHPMatsuri 2010にも講演者として来日されたLithiumリードデベロッパの@nateabeleさんが3年近く前(2008年3月)に書かれた記事です。写真の一番右に写っているのがNateさん。(写真は今年10月のPHPMatsuriより。lllnoriko4lllさんご提供)



Model, Model, Model







CakePHP Advent Calendar 2010でつながる

MVC設計に慣れて、Modelにロジックを移していく作業が進むと、Modelが肥大化していくというまた別の悩みが生まれてきます。そういった悩みへの解の一つが、3日目のshin1x1さんのエントリで紹介されている「4. アクション毎にModelを作る」っていう方法だったりします。

CakePHP Advent Calendar 2010で読めるノウハウは、これから更につながっていくんでしょうね。始まったばかりのこのお祭りが、どこでつながってどう進んでいくのか、楽しみでしょうがないです。



Related Posts

今日から始めるJenkins CI(PHP, Windows, XAMPP使い向け)

ちょっとだけ読みやすいnode.jsからmongoDBへの接続のコードの書き方と、ハマったところ / (A Bit) Nicer Node.js Code To Read To Connect DB, and other small tips

日本人の英語 / マーク ピーターセン


about me

@remore is a software engineer, weekend contrabassist, and occasional public speaker. Read more