摘譯 <Professional CodeIgniter> 的 Introduction

Wrox 出版的 <Professional CodeIgniter> 對我學習 CodeIgniter 甚有助益,尤其它的 Introduction 更是反應了程式人員的心聲。此書沒有中譯本,應該也不會有中譯本了,因為拖得越久,內容就越是過時,這是程式書籍的宿命。而我相信 Wrox 也不會為新版的 CodeIgniter 發行此書的新版本,因為這本書名稱中的 professional,在很多人看來頗為名不符實。不過我喜歡它的 Introduction 的故事,所以就試著翻譯出來,供大家參考。或許,你看過這段文字,會對這本書的價值有不一樣的看法。

When I first encountered CodeIgniter, I was pretty much at the end of my rope.

當我首次碰到 CodeIgniter 時,我可說是處於窮途末路之際。

I'd spent seven years building up a thriving little web development group and had hitched my little wagon to various technologies (like SMARTY and PEAR), and things had run very well for a long time. And then it happened. What is "it"? Well, "it" wasn't exactly any one thing that you could point to and say, "There, that's the problem right there!" It was more like a combination of things. Regardless, "it" hit me with the force of a torpedo, and I felt my professional outlook implode.

我花了七年的時間建立一個繁榮的小型網站發展群組,並為我的小發財車配備了各種技術 (像是 Smarty 和 PEAR),事情在很長的一段時間都運行得不錯。然後,它發生了。“它” 是什麼? “它” 不是一件很確切的一件事情,你無法指出來然後說,「在那裡,這就是問題所在!」它比較像是一些事物的組合。無論如何,“它” 以魚雷般的力量來侵襲我,讓我覺得我的專業的前景破滅了。

I've shared my story with lots of developers over the past year, and they all seem to agree that in the life of every web geek comes a moment of clarity. This happens to us whether we're the worst decision maker in the world or some kind of digital wunderkind.

在過去的一年中,我和很多開發人員分享我的故事,而且他們似乎都同意,在每一個網站怪胎的生命中都曾有過這一清晰的時刻。我們所有的人都曾這樣過,無論我們是世界上最差的決策者或者是某一類的電腦神童

What am I talking about? I'm talking about some kind of tipping point where you look at everything you've done, and you don't want to deal with it any more. I'm talking about making one more really great choice (about architecture, performance handling, security, blah, blah, blah) and putting it in the box with all your other "great choices," and what you end up with is a box full of complexity, a kind of reverse Pandora's box of confusion and mischief.

我說的是什麼呢?我在說的是某種引爆點,你會看著你所做的一切,而你卻完全不想再去管它。我在說的是要做一個真正的更重大的選擇 (關於架構,處理效能,安全,等等諸如此類的東西) ,並把它和你的所有其他的 “重大選擇" 放在同一個盒子裡,最後你所面臨的就是一個充滿複雜性的盒子,一種裝著混亂和惡作劇的反向潘多拉盒子。

The upshot is pretty simple. You end up with extremely complex code that you can't understand. Your mind can't stretch around all the hundreds (if not thousands) of little changes and one-off decisions you've made. You can't even follow all the includes, the third - party modules, and the classes embedded within each other like Russian Dolls. In point in fact, you end up with the Thing That Cannot Be Uttered: extremely complex code you don't even want to deal with.

結局相當簡單。你所面臨的是你自己無法理解的極其複雜的程式碼。你的頭腦無法關注這數百個 (如果沒有數千的話) 小變化再加上再加上你剛剛所做的決定。你甚至無法追蹤所有的 include 檔,協力廠商的 module,以及彼此互嵌的 class,這簡直就像一個俄羅斯娃娃一樣。在這時候,事實上,你所面臨的是無法說出口的事:你一點都不去碰的極其複雜的程式碼。

And the customers that come with that code, who quite naturally want you to make things better, suffer from it. Big time.

然後,伴隨著這程式碼的客戶,他們很自然會要求你把事情做的更好,這又讓你苦上加苦。喔,真是太棒了。

So it was with me, as I contemplated the tens of thousands of lines of PHP I'd written with such good intentions. If you've ever had the pleasure of working with PEAR and SMARTY (and other similar technologies), you know that they solve a lot of problems … but they also bring a heinous level of complexity to even the simplest little web application. And all of it was hanging around my neck like an albatross, and boy, did I want to cut that sucker off and start over again!

我也正是如此,當我看著我自己為了種種目的所寫的數萬行的 PHP 程式的時候。如果你曾經有幸使用 PEAR和 SMARTY (以及其他類似的技術) 來開發程式,你會瞭解它們是解決了不少問題...... 但它們也帶來更可怕的複雜性,即使是最簡單的小型網站應用程式。這一切就像擔在肩上的千斤重擔,喔,天啊,我想要把它甩掉,但它卻像吸盤一樣又會再跑回來!

But where could I go in my hour of need? Who could help me? Was there any way out of the

complicated Rube Goldberg machine I'd built as my little fortress of certainty? At the time when I was feeling the most despair, there was a lot of media exposure for Rails. Hmmmm. Could this possibly be the answer to all my problems? I bought a few books and started playing around a bit. I liked Rails — it has a cleanness to it that makes you feel all giddy when you look at it, like buying a slick new sports car and driving around town with it so everyone can see how fabulous you are. It was easy to use, too, and you could get things done really fast.

但在這需要的時刻我可以往那走呢?誰可以幫我呢?有什麼方式可以讓我離開我所建造的複雜的魯布·戈德堡機械,這個給我小小的肯定的堡壘?在我感到最絕望的時候,有許多關於 Rails 的消息流傳著。嗯,這可能會是我所有問題的答案嗎?我買了幾本書,並開始試用了一下下。我喜歡 Rails — 它很簡潔,當你看著它時會讓你感到驚豔,像是買了一部漂亮的新跑車,開著它在巿區轉,每個人都可以看到你是多麼拉風。它很容易使用,也可以讓工作非常快速完成。

But then, suddenly, the first-date warm fuzzies started wearing off. I realized that I was a PHP

programmer and that I'd spent years developing solutions in that world. There were very few gotchas left, the learning curve was nice and pleasant, and it had been years since any of my customers had felt like I was learning on their dime. I realized, suddenly, that Rails wasn't going to do it for me and my situation. In other words, I realized that I didn't want a glamorous little sports car — I needed something a bit less showy and a lot more functional.

但在這之後,突然的,第一次約會的溫柔朦朧感開始消逝。我意識到,我是一個 PHP 程式員,而且我在這個領域開發程式已經消磨了好多年的時間。PHP 的問題已是極少,學習曲線很容易也很愉快,而且由可能有客戶覺得我是拿他們的錢來練功開始,至今我也用它好多年了。突然,我意識到Rails 不適合我和我的狀況。換句話說,我意識到,我需要的不是一輛迷人的小跑車 — 我需要的是少了幾分艷麗但卻具有更多實在功能的東西。

Yet there were lots of appealing things about Rails. The MVC framework brought simplicity and

organization to the game. The convention-over-configuration approach made the code nice and lean. Where was something like that in the PHP world? I started my search, and I immediately found CodeIgniter.

然而 Rails 還是有很多引人注目之處。MVC 框架為這遊戲所帶來簡易性和組織性。convention-over-configuration 的作法讓程式碼變得漂亮且精簡。在 PHP 的世界中到那裡去找像這樣的東西呢?我開始了我的搜尋,而且我立刻就發現了 CodeIgniter。

In fact, I had CodeIgniter pointed out to me by one of my employees. CodeIgniter was still in the 1.0 phase at that point, and since I wasn't feeling too good about that, I started playing around with Symfony and Cake and some of the other PHP frameworks. None of them suited me, and so I came back to CodeIgniter around the time that it hit version 1.5.

事實上,早就曾有員工向我提到 CodeIgniter。當時 CodeIgniter 還在1.0 的階段,因為我當時並不覺它很好,我開始嘗試 Symfony 和 Cake 以及其他的 PHP 框架。這些框架沒有一個適合我,所以大約在 CodeIgniter 出到1.5版的時候,我回頭來嘗試 CodeIgniter。

Boy, am I glad I came back to it! It was like taking a really long trip around the world and coming back home to a good friend or loving family. It did what I wanted, right away, without all the complexity and baggage. I hardly even needed to spend time with the documentation, it was so intuitive.

孩子,我真高興我能回到它這兒!這就好像繞了世界一圈,然後回到家在好朋友或新愛的家人身邊一樣的感覺。它正是我所想要的,全然沒有複雜和累贅的東西。我幾乎不用花時間在文件上,它是那麼的直觀。

I built my first customer application in CodeIgniter about six weeks after doing that first series of test runs. Over the next year, I built a dozen more, ranging from timesheet applications to scheduling apps to microsite managers and community boards for parents of children with special needs.

在進行了開頭的一系列的測試之後,我花了大約六個星期用 CodeIgniter 開發了我的第一個應用程式。在接下來的一年,我開發了十多個應用程式,應用程式的範圍包括工作時間表、調度應用、小型網站管理應用,以及特別為孩童的家長建立的社群網站。

The results so far would give even the most hardened cynic food for thought. Instead of spending countless hours debugging needlessly complex code and dreading every call lest it be a request for yet another upgrade or change, I can now build entire applications in a weekend. Supporting them is a breeze, because the code is always organized in an easy, intuitive manner. Just about every time I receive a request, there is a CodeIgniter library or helper to ease my burden — or I'm able to write one for myself or reach out to the CodeIgniter community to find help.

截至目前為止的結果,可說在想法上帶來全然的改變。我不再花費無數的時間來對複雜的程式碼除錯,也不再每次接到電話都擔心是不是又一個升級或改變的需求,現在我可以在一個週末建構整個應用程式。而且維護應用程式變得是件輕而易舉的事,因為程式碼的組織架構簡單且直觀。就算每當我接受一個要求時,我也可以找到有用 CodeIginter 的 library 或 helper來減輕我的負擔 — 不然我也可以自己寫一個或者到 CodeIgniter 的社群尋求協助。

In fact, I can now easily build something in a few hours that would have taken me days or weeks with just plain old PHP. It's getting to the point where I've had to rethink my entire billing process, because there isn't much sense in charging so little for a piece of software that helps to launch a significant part of a company's online presence.

事實上,我現在可以輕鬆地在幾個小時內弄出一些小東西,而這在以往只使用單純的 PHP 時可能要花上好幾天或幾個禮拜。它讓我不得不重新考慮我的整個計費方式,因為開發一個程式來幫助一個公司建立一個完善的線上服務,卻只收這麼低廉的費用,實在沒有道理。

From a non-financial standpoint, my relationship with clients has changed. Instead of being an

adversary (saying, "We really shouldn't do that," when I really was thinking to myself, "Oh please, I don't want to do that!"), I became a collaborator, a helper, a guide. In other words, I'm able to fulfill my role as consultant. Not only that, but by spending less time with minutiae, I can spend more time with strategy and more meaningful conversations that help my customers move closer to their goals.

從非財務的角度來看,我與客戶的關係也產生了變化。我和客戶不再是對立的 (當我說,「我們真的不應該那樣做的,」,其實我在心裡是對自己說,「哦,拜託,我不想要這樣做!」),而是成為一個合作者,一個幫手,指導者。換句話 說,我是在履行作為顧問的角色。不僅如此,因為花在細節上的時間變少,我可以花更多的時間在策略和較有意義的對話上,幫助我的客戶他們的達到想要的目標。

And then, in the Fall of 2007, I thought to myself, "We need a book on CodeIgniter!" Not just any book on CodeIgniter, and not just a repeat of the online documentation, but one that will walk a developer through all the phases of a project, from initial client meeting to launch.

然後,在 2007 年秋天,我心想,「我們需要一本 CodeIgniter 的書!」不僅僅只是一本關於 CodeIgniter 的書,也不僅僅只是一本線上說明的翻版,而是,可以帶領開發者走過一個專案的所有階段,從最初的客戶會談到系統上線。

That was the genesis of the book you're holding in your hands right now. At the end of the day, I wanted to craft a book that would take you from point A to point Z (and yes, we might be skipping a few of the less interesting points along the way) and give you a working knowledge of the relevant CodeIgniter pieces as we went.

這就是你現在你手中拿著的這本書的成因。總之,我想要精心撰寫一本書,能夠帶領你從A點到Z點 (是的,我們可能會忽略途中一些不太有趣的地方),並且在這過程中,提供給你在開發程式時所需的關於 CodeIgniter 的知識。

With any luck, I've achieved that goal. I've had a lot of help and feedback along the way, of course, but ultimately, I wanted to show you how to navigate a typical project, complete with customer meetings, iterative cycles, and detours. I wanted to show you how to use CodeIgniter along with agile development processes to build a functional web site — hopefully, by the time you're done with the book, you'll have everything you need to create your own projects.

運氣還不錯,我完成了這個目標。在寫作過程中我得到許多協助和回饋,當然,最終的目的,我想告訴你如何進行一個典型的專案,與客戶洽談,循環的週期,以及讓 你少走一些冤枉路。我想告訴你如何使用 CodeIgniter 配合敏捷開發流程來建立一個運作的網站 — 希望在你學習這本書之後,你會擁有建立你自己的專案所需要的一切。

Here's to building great web applications, and here's to making customers deliriously happy! Most of all, here's to seizing the reins again and taking control of our professional lives!

這個可以讓我們建立了不起的 Web 應用程式,並且這個可以讓客戶無比高興!最重要的是,這個可以讓我們再次握住韁繩,再次取得我們對職業生涯的控制!