swirl
Home Software Blog Wallpapers Webtools
Yii - a PHP MVC framework
Saturday 26, May 2012   |   Post link

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. Yii Logo 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.




Load Comments

Posts By Year

2016 (6)
2017 (5)
2012 (2)
2013 (4)
2014 (2)
2015 (18)

Posts By Category

Software Engineering (1)
IIS (1)
T-SQL (1)
Git (1)
Technology (1)
Database migration (1)
PHP (1)
Corporate culture (1)
Tablet (1)
OData (1)
Containers (3)
Lucene (1)
Test Driven (1)
Java (2)
TDD (1)
Solr (1)
XML (1)
TSQL (5)
Windows (4)
PowerShell (1)
Software development (1)
Wallpapers (1)
Utilities (1)
Entity Framework (2)
Rants (5)
Life (5)
Programming (13)
Office (1)
Book review (3)
Sql Server (1)
ASP.NET MVC (4)
Android (1)
Desktop (1)
Unit Testing (1)

Posts By Tags

Dockers(2) Books(1) Organization(1) Busy(1) PowerShell(1) PHP(1) Backup(1) Database(1) Containers(3) Mobile Apps(1) Paths(1) SSH(1) Web Development(4) Yii(1) Debugging(1) Advertising(1) Adults(1) GradleApache(1) C#(2) SQL Code-first Migration(1) Developer(1) Consumerism(1) Log4J(1) Self reliance(1) Company culture(1) Entity framework(1) Work culture(1) Programming(1) Management(2) Book review(2) Validation(2) C++(1) Sql Express(1) HierarchyID(1) Lucene(1) IIS(1) Windows 10(1) Cmdlet(1) Intelli J(1) JavaScript(1) Spirituality(1) Migration history(1) Tablet(1) OData(1) TDD(1) Data-time(1) Beliefs(1) Checkbox(1) CTE(1) CSRF(1) JSON(1) Windows 8.1(1) Quality(1) XML(1) ASP.NET(4) TSQL(3) File history(1) Windows(1) Wallpapers(1) SD card(1) File copy(1) SQL(1) Windows 2016(2) Python(1) MVC(4) Url rewrite(1) Solr(1) Modern Life(1) System Image(1) Android(1) Commons(1) Office(1) Anti-forgery(1)