Friday, January 30, 2009

The Ultimate Windows PHP Development Environment

So, you are a PHP developer and you have windows. You've tried all kinds of different editors and setups on your system, but nothing works the way you want it to. Well, your answer is here. I've compiled a list of applications that are essential for PHP development.

Hardware

A Computer - Developers do not need fancy machines to develop. They just need something within the past few years that has enough ram to run modern browsers and a bunch of apps open at the same time. I would suggest a minimum Windows XP. Vista will do as well.

2 Monitors - While a single monitor is nice, having a large area to work is also very helpful. What I suggest is adding a video card that supports two monitors. Then plug the monitors in side by side, and configure them.

The reason I suggest 2 monitors rather than having a wide-screen display is because I would suggest that you virtually stack your monitors so that one screen is virtually "above" another.

Why above one another? Simple. Programmers are lazy! 1) By having your monitors placed above one another you simply need to slide the mouse up to get to the second screen. The advantage of this is that the mouse never needs to leave the mouse pad. 2) Having the monitors setup side-by-side makes closing programs more difficult. If I have my browser maximized on screen 1 and I use the mouse to close it, I move the mouse to the right wall, go up to the x and click it.


Basic Software Tools

Browser(s) - Of course you need a browser or two. Being a PHP programmer doesn't always require that you have awesome xhtml/css design skills, but it's worth at least having several different browsers available to test your system on. There are a number of options available, however a simple set of browsers would be most useful.
  • Firefox - This is a Must! If you don't use firefox the only excuse you have is to be using Google Chrome. Other than that, there is No excuse. Firefox has a few nice extensions that make web programming helpful. Often times we will need to integrate PHP back-end with Ajax, or something. For testing purposes in this situation, it would be helpful to add Firefox Firebug.
  • Google Chrome - As mentioned earlier, Google Chrome is a good alternate to firefox. It is still relitavely new, but has some good potential.
  • Internet Explorer - Ok, yes you need it. Not for browsing, but for testing. It is useless to have a site that looks great on Firefox when 90% of your users are using Internet Explorer. In addition to this it is important that you have multiple versions of IE. I would highly suggest: Multiple IE.
PHP Editors - I'm certain this may be one of the more contriversial sections of this post because everyone seems to have their own preferences for editors. However since I'm posting this, my editor picks are best. :-p You need different editors for different reasons. There are two primary editors I use for different reasons.
  1. Notepad++ - This is by far the best editor I've used in a long time, and it is my favorite. Why you may ask? I judge editors by a number of factors. 1) How little memory they use 2) Easy to use Tabs 3) Highlighting 4) Useful features. Notepadd++ has ranked #1 for all of these factors in my book. Here are a few things I like about it.

    - The Light Explorer plugin makes browsing my directories quick and easy.
    - It comes with the ability to edit files directly on a web server through the ftp_syncronize plugin
    - It opens very fast, takes very little memory, has nice highlighting, and tabs at the top.
    - It opens and closes function tabs letting me see only the code I'm working on at the time.

  2. gvim for Windows - Before coming across Notepad++ I used gvim for windows exclusively. Why? Having a background of using the command line, I learned Vi. When you ssh over to a server and need to edit a file, Vi is by far the best editor (sorry emacs lovers). Well, since I'm always using :wq or %s/.../../g and other vi commands, I found that using VI in a windows environment was great. It allowed me to quickly edit files and use my favorite vi commands.

    Why did I switch? Mainly because of tabs. While I know the basics of VI, I never could setup a good solution for adding and managing tabs. I know there is a VI solution, but I fell to the temptation of opening 10+ Vi windows while working on a project. The transition to Notepad++ was a careful one, but I still keep Vi around.

    Why should you consider keeping Vi around?
    - It again is light weight and fast.
    - It offers nice highlighting.
    - It's VI! So you have the great features of VI.

    What can you use it for?
    - Various projects with CSV Files or Excel files. For instance you can turn an excel sheet into a csv file by copying the data to VI and running the following command:

    %s/\t/,/g

    - Vim is great and fast for formatting data quickly and on the fly.
    - The latest version of VI can open very large files quickly.

    Oh, and don't forget, it's VI!
Other Editors - for those of you who are just getting into PHP or if you need help for function names, or if you want to pay for your editor, I suppose you could use apps like Dreamweaver or SciTE or Eclipse with PHP.

The Applications

PHP, MySQL, Apache - This is commonly known (on windows) as WAMP installation. You will need three basic applications for 90% of your PHP development.
  1. PHP - Of course you will need PHP. This is the application that you actually use to run the code you're developing. I would suggest keeping a browser open and making use of the PHP function search box on the top of the php site. I would not suggest installing php alone on your machine unless you have the time to configure it yourself. (see below for xampp installation)
  2. Apache - Apache is the most popular and most stable webserver out on the market. It is worth learning, because if you go to work for a new company, chances are that their web servers use apache. (again, see below for installation)
  3. MySQL - There are a number of databases out there, but by far, mysql is the best for mid or even high traffic sites. For many years it has been second to Oracle, but recent versions have made it highly competitive to Oracle. And of course, you can't beat the price
So what is the best way to get these apps onto my windows machine? I've tried a number of applications that try to tie php, apache, and mysql into a windows environment where it is seamless and allows you to not have to install individual elements. By far, I have found that Xampp is the best of the best.

Xampp is an easy installation process that installs a fully working and functional WAMP server onto your system. It comes with a handy little control panel to allow you to stop and restart the apache and mysql processes. Not only that, it comes with PHPMyAdmin pre-installed to allow you to quickly get your database up and running. Note: Besides the default install, I would suggest making one additional modification to your xampp install. Open C:\xampp\apache\conf\httpd.conf in your favorite editor (Notepadd++ of course) and uncomment the line that starts with "LoadModule rewrite_module"... around 118 or so. This will turn on Mod Rewrite to allow a number of applications work with/nice/urls.

Frameworks

CakePHP - A while back I spent some time looking into frameworks. Out of all the frameworks I came across, CakePHP stood out above all the rest as a fast, reliable, well-designed framework. After spending a year and a half writing code in CakePHP, I've found that it is one of the most flexible frameworks I've used. Since then I've released over 20 sites using CakePHP that get hundreds of thousands of unique visitors per month. It has bee completely stable and reliable. It also has a growing community of developers who are constantly coming up with different uses, plugins, and add-ons.

Forgive me for not adding other frameworks, but feel free to explore your own.

Useful Utilities

VirtuaWin - This is a Must have. I keep a copy of this program on a zip drive in my wallet and install it on any computer that I'm using. Basically VirtuaWin allows you to have up to 20 desktops on your computer. You setup your shortcut key () to navigate to the desktop you are working. This allows me to work on several projects at the same time along with having one reserved for email and music controls. Once you use this, you'll never go back to a single desktop. Plus if you have dual monitors, you double your desktop capacity up to 40! Yikes!

CoreFTPLite - Of course you need an extra FTP utility. I found that Core FTP Lite does everything I want it to do. Fast loading, fast transfers, SFTP, doesn't crash, easy to use, etc. What else can you say? Filezilla is nice too.

Putty - Never go anywhere without putty. Putty is a small executable terminal window that allows you to ssh anywhere on the web. You don't install it, you just open it. I usually copy it to program files / putty and create shortcuts to it for consistancy.

RealVNC - If you have multiple windows computers and again you are too lazy to get up and log into that computer, VNC is the way to go. Vnc lets you open the desktop of another machine on your computer. It's a fairly simple setup, and works great.

Windows XP Tricks to save time - This is a video that suggests additional cool features to help you save time when programming on windows. I use a few of these techniques as well.

And Finally... The best for last...

Git

Git - I put git in its own category because it's so awesome and it is a must-have for ALL development. Git is a version control repository, but far better and faster than CVS or SVN. Git allows you to store snapshots of the files you are working on and easily recover bad changes. Take a look at one of my previous post about using Git with SVN for help in this area.


The End.


Tuesday, January 27, 2009

Unit Testing Cookies in Cake PHP


One problem I came across when building unit tests was trying to check to see if my cookies were setup properly. (Yes I still use cookies.) The problem with trying to unit test a cookie in Cake PHP is that you are only allowed a single page load for the test. A cookie doesn't exist until it is sent to the browser and the browser sends the cookie back with a new page load.

My first thought was to place a conditional in the test case. This would basically be something like:

if (!empty($_COOKIE['id'])) {
$this->assertTrue($_COOKIE['id'] == 100);

}


This method would require you to have to reload the page to properly test cookies. The first page load will consist of 0 tests. The next page load will consist of 1 test. But this method didn't seem like the best way to go about it.

Then I discovered the PHP function: headers_list().

Headers List will return all the headers that you are about to send to the browser in an array format. This was the solution!

Now I can test what the function is expected to send to the browser (which is independent of the browser actually accepting the cookie or not). Here's a sample of what I did.

function testStoreCookie() {
$this->Controller->storeCookie(100);
$header_list = headers_list();
$cookie_true = false;
foreach($header_list as $item) {

$cookie_true = is_string(stristr($item, "Set-Cookie: id=100")) || $cookie_true;
}

$this->assertTrue($cookie_true);
}


There you have it! You can now test setting cookies without reloading the page.

Tuesday, January 20, 2009

Cheat Sheet Collection

Programming Cheat Sheets

Have you ever been programming and simply forgotten a command? When ever that has happened to me, I've had to go to google or the api website to find documentation, and many times that takes longer than I want. Then I discovered programming cheat sheets. Programming cheat sheets are ethical, time-saving, tree killing documents that have commonly used functions, procedures, commands, or examples to jog your memory when programming.

For your convenience, I've put together a zip file with 13 Cheat Sheets. Below is a description of some of the ones you will find in this collection.

You may download it here.



CakePHP Cheat Sheet

The CakePHP Cheat Sheet is helpful for all of your baking needs. It includes naming conventions, common model commands, controller commands, view commands, helper properties, globals, and component information.

Cake can be found at http://cakephp.org






HTML Character Sets


The HTML Character Set Cheat Sheet is quite useful for finding out what the & character is or for escaping "'s. Also it is useful when using template engines that have problems with characters like { or $.











HTML Color Chart Cheat Sheets

This is an essential for designers or when you are coding CSS on-the-fly. It is also useful when picking colors for a new website.



CSS Cheat Sheet

Most web designers have their favorite CSS documentation site bookmarked. Many use reference books to lookup css attributes. This cheat sheet is useful for both novice and experienced CSS'ers to serve as a reminder of the names of css attributes.






Git Cheat Sheet

Git is a great repository system that allows you to archive all of your work quickly, and be able to keep your large project organized. Similar to SVN or CVS, Git offers a local repository as well as public repositories for sharing code. The cheat sheet is very valuable as a reminder of what commands to use for using Git.




HTML Cheat Sheet

Are you often forgetting what HTML is able to do? This cheat sheet has a list of much of the markup that you may not normally use, as well as those old favorites.





Also included are:
  • Javascript Cheat Sheet
  • Jquery Cheat Sheet
  • Mysql Cheat Sheet
  • Php Cheat Sheet
  • Prototype Cheat Sheet
  • Regular Expressions Cheat Sheet
The full download is available here.

Feel free to post additional cheat sheets in the comments section.

Friday, January 16, 2009

Four Ways To Retrieve Model Data

There are a number of ways (in addition to direct model access from a controller) to retrieve data from a model. The type of method you use depends on what you are attempting to do. Here is what I've discovered.

$uses array

As you recall, the standard way a controller defines its models is by using the variable $uses.

$uses = array('Post','Comment');

This works fine for most cases, but what happens if one action in your controller needs to use the UserNotes Model, but no other actions in the controller needs it. One way to solve this would be to simply throw it into the $uses array().

$uses = array('Post','Comment','UserNote');

Now in your controller action you can call $this->UserNote->find... to get the data you want to get.


ClassRegistry::init

Recently, after browsing through the regular CakePHP blogs, I came across an article about building a dashboard in CakePHP. This article introduced the available function called ClassRegistry::init(). I found it to be an interesting addition to my growing Cake PHP toolset. ClassRegistry::init() is the actual method that a controller uses to load its models (you can find the call in the cake controller class in the function loadModel).

When to use $uses and when to use ClassRegistry::init

There may be times when you do not want to use the $uses array. In the above example, UserNotes will be loaded for all of the actions (even the ones that never call it). This is a problem because the more models that that single action needs, the more strain it will put on all the other actions. The question of when to use ClassRegistry::init is: Are you willing to have all of the other actions suffer from the performance hit of loading unused models?

This is where ClassRegistry::init() comes in to play. ClassRegistry::init() will load a model directly, skipping the standard model caching, from within the action. This means only the action you are running at that moment will have UserNotes loaded. All of the other actions in that controller will not load that model.

function getUserComments() {
...
$userNote = ClassRegistry::init('UserNote');
$userNotes = $userNote->getUserNotes($user_id);
...
}

When should you use ClassRegistry::init?
  1. When you have an action that calls models that other actions do not need.
  2. When you need to retrieve unrelated model data.
  3. When you want to skip model Caching and access the model directly.
Model Relationships

Many times both of the previous methods can be avoided because of natural model linking. Remember, when you define a model, you also setup relationships. The controller/action can use those relationships to retrieve data, as seen in Mark Story's Blog post.

If you are requesting information from a joined table within the relational chain, calling models through their relationships would be the best methodology.

$uses = array('User');
...
$this->User->Notes->find('all');

Although this method is limited, it is powerful, and it prevents redundency. By default a model will load the models it is associated with. So when you want data from an associated table, don't bother defining it in the $uses array, because Cake will automagically load that model and make it available through your current model. The key to making this work effectively is good database design.

requestAction() a Last Resort


The tackiest way to retrieve model data is to use requestAction. The reason for this is because of how Cake works. As you can see from the diagram, the dispatcher determines the route and then loads a single controller along with its models. Using a request action will cause the dispatcher to run again (possibly re-running routes) and then loading your additional controller, along with its models. This can cause a performance hit without caching.

Inside your Users controller you have an action called show_notes(). show_notes needs to access the notes table and retrieve a list of notes.

// Users Controller
function show_notes() {
...
$notes = $this->requestAction('/notes/get_notes/'.$user_id);

}
// Notes Controller
function get_notes($user_id) {
return $this->findByUserId($user_id);
}

There are a number of problems with this type of design.
  1. Additional server load for every request, which may cause bottlenecks.
  2. It goes against the "fat model, skinny controller" concept
  3. You are redefining the controller to do what the models are supposed to do.
Summary

Put simply, there are plenty of options to retrieve model data. The method that you use truly depends on your specific problem that you are addressing.

Thursday, January 08, 2009

Using Git with SVN

Although git might generally be recognized as Southern slang, in web developing, "git" is one of the most useful applications for your local development environment. Git is a version control system similar to SVN or CVS, however using git is a bit different SVN or CVS. In this post, we will assume that you are familiar with version control systems, and you are currently using or have used SVN.


Learning Git

There is usually a little learning curve when looking into a new concept. Git is no different, however it is well worth the time investment to learn, because it will certainly save you time in the future. Here are the pro's and cons of learning git:

PRO's
  • Increase productivity and speed in local developing - Git is fast and it works locally. Git allows you to take an entire project, copy it, totally trash it with debug code so you can get the task done, and then completely restore the original files almost instantly.
  • Less hassle - Git's commands are purposefully easy. With one command you can commit all of your changes. With another single command you can create a new branch and immediately start working.
  • Keeps you on track and focused - with the branching functionality of Git, you can work on two or three projects in the same code base at the same time. Switching between your code is almost as easy as Alt-Tab in windows.
  • Git is powerful - Git's merging functionality is very quick, simple, and powerful. There are conflict resolution tools that help you to merge safely without breaking things. It also allows you to go back and fix mistakes and recover lost changes.
  • Git is local - The entire repository for Git is contained in a .git directory in your root path. This means if you have a project with several hundred directories, you won't have hundreds of .svn or CVS directories. Just one .git directory.
CONS
  • About the only con of Git is the intimidation factor. Currently there is no TortoiseGit to make the familiar transition from TortoiseSVN. The best way to use git on windows is to use the command line using MsysGit.
Where To Start

Screencasts and tutorials are some of the best places to start to familiarize yourself with something new. Here is what I did to learn git:
  • Watch some screencasts - I started out with the free screencast from debuggable. It gives you a step by step process on how to setup Git along with examples of building a repository.
  • Read the Documentation - Yeah I know, writing code is much more fun than reading about it, but hey, it's worth it. I would suggest reading and focusing on the first 3 chapters of the Git User's Manual. It will provide you with specific walkthroughs and examples of each concept.
  • Do it yourself - Now you should be ready to start implementing git in your own projects.
Development Process

Here is a development process to get you started with git with a project that you are already working on.
  1. Open the git bash window.
  2. Navigate to the root directory of your project.

    cd /c/www/myproject

  3. Set up your git repository in that folder.

    git init

  4. Set up any .gitignore files that you may want. Often this would be for tmp folders, dynamically generated files, or any .svn or CVS folders.
  5. Add and commit everything

    git add .
    git commit .
    or
    git commit -a .

  6. Create a new development branch and switch to it.

    git checkout master
    git branch t.setup_test_cases
    git checkout t.setup_test_cases
    or
    git checkout -b t.setup_test_cases master

  7. Now you are safe to start coding your task. You are now working on a copy of your files, everything is backed up and recoverable. Add, Delete and Commit as you go, and everything will be stored to the branch.

    7a. At any time during development you can repeat step 6 to create an additional branch to get a high priority item finished, then go back to your main project. You can also have two or three simaltanious projects open at once.

  8. When you're done with your changes to a branch, simply merge back to the master.

    git checkout master
    git merge
    t.setup_test_cases
    #Resolve any conflicts here
    git status

  9. Use gitk to examine your changes to see what happened.

And it's that simple.

Using Git with SVN

Now that you know the basics of git, you can implement it alone, or within your SVN repository. Git only adds a single directory .git within the base of your application and it does not interfere with SVN. From the Git side, you will need to filter out .svn directories. The implementation is easy enough. Simply add the following line to the file: ".gitignore"

.svn

Basically the .gitignore file will allow git to ignore any matching file or directory. You can use wildcards to find more specific file matching.

At this point you will be able to use the git steps above within an SVN repository to keep up with your local changes without having to commit to the svn repository as much.

Why Use Git in SVN?

Using git inside of SVN has its advantages and disadvantages. One advantage is you gain the flexability of simple branching without having to download the entire repository from the SVN server. It is like adding tabs to a web browser. You can multi-task, branch and merge all locally, and then once your modification is tested and ready, simply SVN commit.

Let's look at an example.

I have an SVN repository setup in my development environment called /www/coolwebsite/ I created the git repository as described above and I setup my .gitignore. I have a 3 item project list for changes on the website.
  • change the design of the home page
  • create an email autoresponder
  • create a contact us form
The first thing I would do is decide on one project to start on and branch off of the git master.

git checkout -b t.home_page_design master

This says that I am going to create a new branch called 't.home_page_design' based on the 'master' copy, and I'm going to switch to that branch.

So 'git branch -l' should look like:

master
* t.home_page_design

Now let's start working on the project. I edit the home page and save it. I edit 2 css files and save them. Then I add 3 new images. I do a 'git status' and see:

$ git status
# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
#
# modified: index.html
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# images/banner.gif
# images/logo.jpg
# images/test.gif
no changes added to commit (use "git add" and/or "git commit -a")

Now simply add and commit to the branch.

git add .
git commit .

If you're using TortoiseSVN you will find that the changes have become red in your file browser. This should reflect the changes that you've done in your files. Before committing to SVN, you first want to merge the changes back to the master branch.

git checkout master
git merge t.home_page_design

When you checked out the master, the red files in TortiseSVN should have turned green, but they should have turned red again when you did the merge. There should be no conflicts here and now the master branch should reflect all fo the changes of your branch. An additonal commit is not necessary.

It is now safe to publish your changes to the SVN Repository.

The next project is to create an email autoresponder. Again we make a new branch.

git checkout -b t.autoresponder master

This time as we start working on the autoresponder branch, our client comes to us and says that they need the contact us form built ASAP, and the other project needs to go on hold. We don't want to lose any of our changes to the autoresponder either. Git handles this quite well.

First commit all of your changes:

git commit -a

Second, create a new branch for the contact us form.

git checkout -b t.contact_us master

Now you can start on the contact us form and still hold on to the changes from the autoresponder. While you are editing and adding files, you will notice your changes reflected in
TortoiseSVN. During the middle of a project you can easily switch branches and work on them without affecting any other branches.

You finish the contact us form, commit it to the branch and then merge to the master.

git commit -a
git checkout master
git merge t.contact_us

Now master has the contact us additions. Commit to SVN from the master branch, and then checkout t.autoresponder

git checkout t.autoresponder

Finish the autoresponder and commit the changes. Commit to SVN, and there you have it.

Final Thoughts

As you can see, there are some great advantages to using SVN with Git, and there are a few disadvantages. If you branch for every change, it will force you to keep tasks separate which could be a good thing or a bad one. There is a little bit of overhead during development to keep both repositories in sync, but the flexability that you gain is sure to outweigh any lost time.

Feel free to comment.