48JIGEN *Reloaded*

Re: Best Practices in MVC Design with CakePHP

2010/12/01

※日本語版はこちら

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

nateabele

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日目です。

3日目のshin1x1さんのエントリ「Modelとの付き合い方」、めちゃくちゃ勉強になりました。CakePHPを使い始めてから、Modelの使い方で悩まなかったことは一度もないです、はい。。。特に3番のDBのビューを使うアイデアと4番のModelの使い方は、今度の休みに試してみたいと思った次第。

そして期せずして4日目の今日も同じModelについて書きます。といっても僕は日曜プログラマでCakePHPをさわるモグリビギナー。大きなことは全く言えないのですが、僕のようなCakePHP/フレームワーク初心者がCakePHPやMVC設計を学んできた過程で一番ためになった記事を紹介します。まだ使い始めて間もない人や、あまりCakePHPに詳しくないけど興味がある人の参考になったら嬉しいなー。

CakePHPを使ったMVC設計のベストプラクティス

nateabele

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

特筆すべきはMVC設計を賢く行うためのノウハウがチュートリアル形式で説明されていることで、その内容は全く古さを感じさせません。残念ながらオリジナルの記事の方はリンク切れを起こしているようなのですが(2010/12/3調べ)、幸いなことに翻訳済みのバージョンが読めますので、こちらをご覧下さい。(@nateabeleさん、@junyaさんありがとうございます!)

記事ではCakePHPを題材として扱っているものの、どんなフレームワークでも通用する経験則になっていますので、MVC設計のノウハウを知りたい方であればどなたでも(PHPに詳しくない方でも)見て頂く価値はあると思います。

Model, Model, Model

記事にはModelについての考え方の至言がいくつもちりばめられていて、今思い出してもヨダレがでてきます。以下はその一例。

これだけでご飯3杯はいけますよね!

簡単で効果的なMVC設計の法則:置けるなら、コードをModelに置く

そして至言だらけの文章の中で、最も強調されている一説がこれ。

恥ずかしい話ですが、この記事を読むまで私が書いてたCakePHP上のControllerのソースコードには、Modelの3倍かそれ以上にも上るボリュームのロジックを実装していました(苦笑)。絶対これメンテナンス性悪いなー、うまいやり方あるんだろうなー(けど探すの面倒くさいなー)と思ってたところで出会ったのがこの記事です。記事が短かったのですぐ読めたのも大きいかも。

僕のように、「MVCかくあるべし」を学ばずにFrameworkを使いはじめちゃった人とか、学習して頭ではわかってるつもりなんだけど、今イチロジックのModelへの入れ方が今のままで正しいか自信が持てないという人に、特に読んでほしい記事です。実際にコードをどのように編集していくかの手順が紹介されているので、読みながら手元でコードを修正することでより理解が深まると思います。

CakePHP Advent Calendar 2010でつながる

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

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

5日目の明日は、@uechocoさんです。明日もModelについてだったりして・・・と勝手にドキドキしてる次第ですが、果たして何についてでしょうか。楽しみにしています!

このエントリーをはてなブックマークに追加

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