PHP Development with Yii

Whilst Yii is an important part of PHP development, it is only a part of a much larger process. I would like to document the techniques I used to develop my latest project for the benefit of beginners and/or more experienced developers. It is a PHP project and I would hope we can discuss/improve the process for the benefit of all PHP developers.

Project Details

URL: http://www.cashflowyourbusiness.com

Purpose: To provide a free cashflow forecasting tool for small business.

Server: Quad-core XEON based in the US

Server OS: Ubuntu 10.04 Server LTS

Philosophy

I wanted a development environment and set of processes and tools that would be scalable. That means, if necessary, if I needed multiple developers working on the project I could easily scale the process and set of tools that I currently use to be rolled out and used in a fully integrated environment.

Also, where possible, I wanted to use open source or at least free software. I didn’t mind paying for software where necessary, but if there was an open source version that could do the job just as well, then I would select it.

OS

In keeping with the open source philosophy, I chose to use Linux as my development OS, specifically Ubuntu 10.10. Let’s not get into a linux distro war here, most flavours of linux can be used. I tend to use Ubuntu for both desktop and server development, however, because the community is so huge that there is always a solution somewhere on the web to any OS related problem I may have. I set the development environment up by installing Ubuntu 10.10 Desktop in a VM (see next section) and installed the separate LAMP (Linux Apache MySQL PHP) components.

Virtualization

In keeping with the scalability philosophy, I chose to do my devlopment on a virtual machine. That way any additional developers that need to do work on my project can just get a copy of the VM and the whole development environment is set up and ready to go. I chose a free VM but any will do. I chose something that is quick and easy to set up as I find the non-graphical solutions such as XEN too difficult to get up and running. The VM I chose is VirtualBox.

The idea behind using a VM is that setting up the environment is very time consuming. Installing all the tools mentioned here and configuring them properly to work together can take weeks. So, once the development VM is set up, you never have to do it again to work on another PHP project, saving weeks/months of time.

UML Design Tool

This was a tough one. There are very few open source options here. I looked at ArgoUML and BOUML but neither one was up to the task. So I considered payed options with Linux options being the preference. Unfortunately again I was out of luck. I settled on Enterprise Architect which, for the features it has, is a very reasonably priced product. Unfortunately, I have to run it under WINE. Fortunately, Sparx Systems who make EA support running it under WINE.

It should be noted that the UML design tool is not used just to document the model. It is used as a repository for the design, documentation and code. I use it for basically everything! Mostly everything… but I will mention these again in the development process section.

Data Modelling Tool

Ideally, I should be using Enterprise Architect for the data modelling as well. Unfortunately, I started designing the database schema well before I was ready to code so I hadn’t purchased EA as yet. So, I used MySQL Workbench to model the data schema. It’s really quite an excellent tool considering it’s free. I have to say though that MySQL is still rather immature when compared to using something like Oracle (which I have been used to using in the past at work). However, all in all, MySQL combined with the Workbench tool provides an adequate and scalable solution. I generate the create scripts from from MySQL Workbench and use shell scripts which I wrote to run those as part of the database creation process (which also includes loading the data and other database objects - see next section).

Reference Data

I store the database reference data in XML files and run them into the database using aforementioned shell scripts. The upgrade script only loads the latest data which I keep in separate XML files in addition to the main XML files. It’s a bit clunky doing it this way and there are better ways (ie the scripts can query the database to determine what data has already been loaded) but I had spent enough time on my scripts and I needed to find a happy medium and go with that.

PHP Framework

Well… Yii of course! When I was looking for a framework, I wanted one which has ORM and generates the full MVC architecture and at the time I was looking, two frameworks clearly and easily fit the bill: Yii and Prado. I chose Yii because I found it easy to understand and use.

I have to say though, the Yii community is very helpful. There were a number of technical issues that I came across both during and after development and I was able to solve them very quickly thanks to the prompt responses from the online chat that Yii has on the website. If they were not able to provide a solution directly, they were able to point me in the the right direction and the rest was up to me - yes, you still need to think for yourself and not expect to be spoon fed.

IDE

I chose Netbeans over Eclipse only because I don’t get Eclipse and don’t like using it. Back when I tried using Eclipse it was too Java focussed and not friendly to other programming languages. I find Netbeans easy to use and configure so that was the tool of choice.

Software Configuration Management - SCM

I was looking for a scalable, distributable SCM tool and old tools such as cvs and subversion, etc didn’t cut it. There were basically two tools that I considered, Mercurial and git. I chose git because it was built specifically for linux and it was developed by Linus Torvalds but again, Mercurial would be just as good if not better. Flip a coin again…

And what do you put under SCM? Just about everything. Obviously your source code but also, tools (I put the framework, graphics libraries etc), scripts, models, reference data xml and anything that is part of the development project.

Also there is a great graphical tool for git called SmartGIT. It’s a free tool for personal use and paid for commercial use. Since I am not making money from my project I have not paid for it but will do so as soon as I decide that I will monetise my website.

Regression Testing

This is an important aspect of the development process and one which can be implemented as a continual automated process. Unfortunately, I haven’t implemented this as yet nor investigated it far enough. There is functionality in Yii to help with this but I haven’t implemented it. More discussion/feedback/investigation is required to document this.

Software Development Process

Ok, so now I have covered all the tools and environments that I am using, how do I use them together to develop an online PHP website? The first thing that should be noted is DON’T JUST GET IN THERE AND CODE! Plan properly and you will end up with a well documented, well designed and well implemented project. Most of the work is in the design, not the coding. The coding is monkey work that you need to do once the design is fleshed out and understood.

  1. The first step is to define the data model. What am I capturing from the user, what data do I need to capture for processing/reporting, etc. There and many steps missing here of course that larger software development houses do as part of their processes not the least of which is documenting the requirements. For smaller online projects, it is assumed that the developer/designer has identified a niche market and understands the requirements that he/she is seeking to implement. So what it all comes down to then is data modelling. Open up your chosen tool Enterprise Architect or MySQL Workbench and start designing your data schema. There are volumes of books written on this topic (similarly for UML design) so actual design methodologies in the data or coding space are outside of the scope of this document.

Once the data model has been designed, just generate the create scripts from the tool. MySQL Workbench and many other tools have the capability to update the db schema from the tool itself. That’s great, but it does not lead to am automated, repeatable process so I don’t use this feature and I recommed other developers don’t either.

Instead, I have built scripts that will take the db create schema that MySQL Workbench spits out and drop the existing db schema and rebuild it from the create script. Then it loads the reference data from the xml definition files. So I can completely rebuild a working database withing minutes via an automated, repeatable process.

I will mention shell scripts a number of times throughout this document. I haven’t mentioned them in a separate section because I consider them to be more like the glue that holds the whole process together and provides automation, repeatability and consistency to the process.

  1. Now you have a database schema all set up ready to go. But a database on its own is practically useless. This is where an ORM framework comes in. It’s great how you can just point Yii to the database schema and it will create the entire MVC code including all CRUD code. Some “massaging” of the code is necessary to get it working the way you like especially with logins etc. This is where I use the Netbeans IDE to make changes and debug.

Job done! Now you have a fully functional website which is capable of user login and collecting/saving/updating of data from the user. Just add business logic to complete the website.

  1. Business logic… This is where the UML tool comes in. I use it for all the business logic. This is also where I document the design/code and where I also keep the code. I enter the code I require in the UML tool itself and just update and regenerate the source files as required. I am not a believer in round trip engineering for two reasons. First, I don’t have enough faith that the tool will faithfully and accurately update the model from the code and more importantly I believe it breaks the OO mentality of the developer. When you are making structural changes to code via source files instead of the model, you are not thinking in an OO way, You are thinking in a procedural programming paradigm. So, that is why I keep all the design, documentation and code in the modelling tool and that also ensures that your design, documentation and code are always up to date and in sync with each other. Magnificent. I really love this part.

  2. Great, now I have a fully functioning website on my development VM. What next? What’s my path to production? Well, this is where my scripts once again come in handly. I have a deployment script which packages up all the code and creates a self-extracting executable. I then ftp that to the production box and run it. Simple. Well, actually, the scripts took some time to put together because there are complications depending whether it’s building an install (this packages all code and tools and database create scripts which blow away the existing database and rebuild it from scratch) or an upgrade (packages up all code and detabase upgrade scripts which update existing schemas and load new reference data).

So, after that high level overview of my development process, tell me about yours, tell me what you think of the way I work, what you like about it, what you don’t and more importantly, how you would improve it. That way we can all benefit and improve the way we develop.

Regards,

Vangelis

NOTE: moved to proper section (Tips, Snippets and tutorials instead of General Section for Yii 1.1.x)

Hi Vangeli,

This is absolutely awesome, thanks for sharing! Recently I started looking for improvement php development process too. Regarding one of the steps I opened another topic UML tool in Miscellaneous. Also, look forward to read more opinions on php development.

Cheers,

Vit

At the moment, I’m a hobby programmer that learned (and is still very much learning) from books and tutorials rather than classes or working in a software company. The thing I have most problems with is steps 1 and 3 in your SDP. I know just a little bit about UML and other prep-work, but not as much as I would like.

Do you have any recommendations for books that don’t necesarily go into the technical things of UML modeling for example (though it would be great if you have), but more in how to organize a project? My own site is pretty small-scale. I have an idea I’m toying with for a much larger site, but I need to get more familiar with organizing things better.

Thanks and great post. I bookmarked it and will probably read it a number of times again.

Hi jodev,

There is nothing better than Martin Fowler’s book “UML Distilled: A Brief Guide to the Standard Object Modeling Language”. It’s a big boost to your professional carrier if you start php programming with right tools and approach.

I studied UML at Uni, but haven’t used it in real web development world and I really regret this fact. Luckily, at my current work I’ve met people who are using UML, but they’re not php programmers. Anyway, at least I’ll try to bring UML to my php development. And I’m glad to see that many php people around the world think in the same way. We’re all supposed to produce quality software products despite the programming language.

Cheers,

Vit

Hi Vit,

Thanks for the suggestion. I will try and see if I can find a copy of it in the local bookstore this week. I noticed it’s a slightly older book, so I just hope it’s still pretty easily available there.