Which language should I use to develop my dream website?
The answer is easy when you are developing a website with static content, say putting up a picture gallery, plain HTML is all you need. However once you want people to interact with you, you need to be able to execute some code on your website. PHP is one of the cheapest options for doing this. Most web-hosting companies offer pHp where you can code using the language (pHp) and generate the HTML content which is finally displayed on the user’s web-browser.
Now most folks who come with a programming background will find it pretty easy to learn pHp even if they have not coded in pHp before. The really hard part is writing the code in a way that is is easily understood even after say one year of having looked at it.
With the above requirement, writing even a single web page which accepts some information from the user, store it to a persistent store like a database and then display the same information in a meaningful way is not easy at all.
Why is writing maintainable code for the web so difficult?
Its because:
- First, there is no state as such. Everything is disconnected. You can't have a variable with some value set in one web-page and expect it to be available when the user navigates to another page
- The entire UI has to be generated every time i.e. some processing needs to happen for every web-page
- The processing can only happen when the browser sends a request to the server
Scripting languages like pHp offer the amazing ability to mix HTML with pHp and generate content on the fly. However, writing any kind of slightly complex code like displaying a list of products really makes the code hard to understand and above all maintainable. In a single pHp page, where do you connect to the database? Where do you display the data read and where do you update the information entered by the user? If a use selects a product in a shopping website should the code to add the item to the basket be in the item listing page or a different page?
There are no straight-forward answers to the above.
Enter the gurus. People have been developing such projects for many years and have discovered better ways to do such things. Recognition of such common situation have given rise to well-known programming patterns and one such pattern is the MVC (Model View Controller) pattern. A pattern is nothing but a set of rules or a way to write code. Frameworks are actual implementation of the rules using code.
Framework is nothing but a boiler-plate code where most of the decisions about where to put what has been taken by the framework author. The author has used his experience to come up with a set of rules which he thinks is the best way to develop – all frameworks have one important aim – this is to force the developer to write code in a specific way to increase maintainability and reuse. Once the basic idea behind a pattern is understood, developing within the framework becomes a lot faster than using the raw language (pHp in this case) directly.
While developing this very website, I went through a lot exciting experiences. Being a C++ developer (a control freak), I stayed away from frameworks and started building the website using pHp directly. The parts of this website which need programming is the ability to accept comments for software I am offering for download. Soon I needed ability to store number of times something has been downloaded. Thereafter I needed a nice way to create entries for things which can be downloaded and such requirements kept growing.
I almost spent a whole week writing pHp code without any framework support. Pretty soon I was struggling with all the questions about where to write what and maintain the slightest sort of discipline. This is when I thought about using a framework – why not a framework? I have been using the MFC (Microsoft Foundation Classes) and Visual Studio code generation/framework for years and most of my projects turned out that way I wanted.
The main reason for me to use a framework was to separate business logic, data and UI presentation. I needed an implementation of “model-view-controller” pattern.
I first started experimenting with the Zend framework. It’s been around for a long time
and is quite well known. There are also lots of books written about it. I read online tutorials
as well as reached almost half of the ‘Zend framework in action’ book.
All well written articles and books but for the life of me, I couldn’t figure out where
to start writing actual code. I even tried using Zend studio which is a development environment
specifically for building Zend framework based web applications. The problem here was that the
code generated by the IDE was quite different from the code presented in the book which again
differed from the code present in other articles and blogs.
I decided to try one more framework before giving up – this is when I chanced upon a relatively new framework on the block called Yii. I tried searching for more reading material but couldn’t find any books on Yii at the usual retailers. I then took a look at four videos which the Yii guys have put up on their website – each tutorial talks about a very specific step in the development of a website.
- How to install Yii
- How to create the first application
- How to display something keeping data and UI separate
- How to interact with data in a database keeping data objects separate
These four tutorials is all it took for me to believe that THIS is THE framework I am supposed to use.
The great thing about Yii is that it is able to generate boiler-plate code which will works out of the box. All you need to do is add your specific changes.
Yii does not need separate IDE for code generation to work, the Yii framework installation itself is capable of:
- generating code to create a web-application which can then be customized
- generating views
- generating controllers
- generating CRUD screens
This is just the tip of the iceberg. Yii has amazing support for almost everything that one
would need to build a great website.
The directory structure used is very intuitive. The help documentation is good and the forum on
their website has very good developer comments which are invaluable.
I believe Yii has taken developing pHp based websites to the next level.
If you are thinking of building a pHp based webstie, I’d strongly advise you to take a look at Yii.