Friday, December 10, 2010

Building websites using MVC in PHP

When you start PHP programming you can write some code very quickly and get some very good results very quickly. Quick as a flash you can learn the syntax in a few weeks (especially if you know another language) and in week 3, you can have a dynamic website spitting out information in a variety of ways. That's fine and it CAN serve you well, but it has its problems: scalability, readability and edit-ability (and many other 'abilities) all become increasingly hard to maintain.

When you learn a language, you don't become proficient in it over night, you have to use the language for months and even years before you come to real grips with it. Each language has its own set of intricacies, pitfalls and glitches that set it aside from its peers.

Even when you crack a language, there are number of different ways you can set your site's architecture. For example I went through these stages (and many in between)

Writing pure procedural code was the method I originally. With the odd exception of including the footer, header and my functions file, I was writing code per dynamic page, top down in a procedural manner and I thought great, we have a site and it works pretty well.

I then started to learn more advanced functions, using mod-rewrite to route requests and various other little tools that all made my life a little bit easier. With each passing stage, you look at your code and you honestly think that each iteration is the best you could ever do. That is until you move in to the next stage of your development where you try something new or read about some new fad sweeping the world of programming.

The next stage was learning, using and creating classes to use in Object Oriented Programming (OOP). When I started using OOP I was astounded how much time I was saving when moving from project to project. I had created a database abstraction layer, form builder class, email class, social media class and many, many more that could be used in all my different projects. Again this method was great, I was a lone developer in the agency I worked for and any changes that needed to be made to the code could be done by myself, no problem. As I became a better developer over time I started to hone my scripts and use different folders to separate sections of the websites architecture and things became a little easier to manage. The problem I think came down to me working on my own when it came to the development side. I was using classes and objects but I was still passing SQL from within the page to the database and letting the same page then take those results and outputting the findings. It was at this point I turned to Ruby on Rails and learnt how to make a basic website using Ruby and things became a whole lot clearer. It showed me a structure of how a web site should be built and really opened my eyes to the MVC design pattern. I also realised that what could be done with Rails could surely be done with PHP.

Once I decided that I was I going to use an MVC framework I needed to decide which one to use and after a little bit of reading I made the decision to use Codeigniter and bought a book on the subject. I built a couple of quick sites and decided that I absolutely loved it and that site and application builds were quicker and much easier to maintain. However I also found myself to have a nagging issue. How did this wonderful thing work, so I took it upon myself to write my own little framework. It was a little bit daunting, but I had my own set of classes which I could integrate which and separating the page requests as per the MVC model. You could say that why create your own MVC framework when there are so many available ones out there? Well, it gives you a great understanding of the MVC model and as you are making something that is comparable to the big wigs in the PHP world like Zend and CakePHP (though may be not as substantial) then you can consider the creation as a sort of rite of passage as a developer.

I am not going to go in to massive detail when it comes to building how you you build your own MVC framework but it runs along these lines:

1. Create a rewrite condition in your .htaccess file that routes all requests bar images, css, js to a PHP file (which is generally known as a bootstrap file)
2. Use a bootstrap file to separate your page request in to 2 basic sections. In my case I took the $_SERVER['REQUEST_URI'] value, used explode() on the '/' character and then accessed using $array[0] and $array[1] to access necessary parts of the URL. When this is done you will take an example like this:

http://www.domain.com/products/widgets

Which when broken down would be pointing to a controller named 'products' and and an action named 'widgets'. Once you have these two sections you can start using autoload to start using files automagically which will include your controllers and actions and calls to your views where data can be passed through to be outputted using HTML.

On to the benefits of using an MVC Framework in PHP

You will find numerous pages on the web regarding the benefits to the MVC. I will add the obvious ones and then a few more of my own. With MVC it helps you build sites in team by allowing multiple people or groups access to specific sections of code. You keep all your presentation, application, data handling code separate. Here are a couple more that, while are probably a little less grandiose will still help when it comes to creating your projects and are some of the major benefits that I have found.

I use a PHP IDE named PHPEd and it works really well with both HTML, PHP and CSS. When using the MVC method I refound that I needed to come in and out my PHP and HTML. It makes it much easier to read and with a flick of a button in PHPEd (F12) you can quickly get an overview of all your PHP and HTML separated out. Front end developers can go in, add their elements and nothing is affected (this comes back to the coding in sections) and everyone is happy.

Code becomes more modular, not only does it make it easier to use in other projects. For example, lets take an FAQ section. Lots of sites will use something like this, why completely rewrite for each site. Copy, Paste, Tweek and your done.

When you need to find some code, you always no where it is. The URL tells you where the code is. I work in Digital Agency and I am constantly working on numerous sites and finding code use to be a real pain. All I can say is: it's much easier now!

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home