Monthly Archives: July 2008

My Experience with Zend Framework

OK, during the last two days, I have built my first mini-application using Zend Framework. Now, I am beginning to understand how this PHP framework functions. I tried to uploaded the application onto Bluehost for the initial testing. But, it did not go as smooth as I had hoped. It took me two full days before getting all the issues worked out.

1st, Bluehost does not support Zend Framework by default. This means that I had to upload the ZF library into my home directory and edit the php.ini file. My test subdomain is test.are4.us. So, in that directory, I have my php.ini file, set up in the following manner.

# For PDO stuff, Bluehost has already gotten that configured. For my Mac, I had to recompile the PHP binary from the source. Apple does not enable PDO support out of the box.

extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so
extension=pdo_mysql.so

zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so
zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so

# This addes the ZF framework library to my PHP lib path. It may require “apachectl restart”
include_path = “.:/home/arefouus/public_html/test/ZendFramework-1.5.2/library”

2nd, here the .htaccess file in the public_html/test/zft/html directory. This will be generated by the Zend IDE; but, requires minor tuning. Its purpose is to turn on redirection the index.php file and enable the Front-Controller pattern.

#Turn Rewrite Rule on.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
Options -Indexes

Lastly, here is my .htaccess file in the root directory – public_html to enable the PHP handling.

# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddHandler server-parsed .html
AddHandler server-parsed .htm
AddHandler application/x-httpd-php5 .phtml

My index.php file is shown below.

<?php

define('APP_DIR', dirname(__FILE__) . "/application/");
define('ROOT_DIR', dirname(__FILE__));
define("WEB_DIR", dirname(__FILE__));
date_default_timezone_set('Europe/London');

set_include_path('.:/library/:/Library/WebServer/Documents/zft/:' . get_include_path());
require_once 'Zend/Loader.php'; // Main Zend loader class
//include("Loader.php"); // Custom loader class //need to investigate
Zend_Loader::registerAutoload();
//echo 'Path is'.get_include_path(); // # important - this echo statement should not be here, or it will cause header sent error.
/* not needed, already loaded by the auto-loading function.
require_once 'Zend/Layout.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Registry.php';
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table.php';
*/
include_once 'application/default/models/Albums.php';
include_once 'application/default/models/AlbumForum.php';

$config = new Zend_Config_Ini('../application/config/config.ini', 'database');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);

// Setup controller
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('../application/default/controllers');
$controller->throwExceptions(true); // should be turned on in development time

// bootstrap layouts
Zend_Layout::startMvc(array(
'layoutPath' => '../application/default/layouts',
'layout' => 'main'
));

// run!
$controller->dispatch();
// it is corrected, that the closing ?> is not needed here.

My IndexController.php file.

<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Db/Table.php';
class IndexController extends Zend_Controller_Action
{
/**
* The default action - show the home page
*/
public function indexAction()
{
$this->view->title = "My Albums";
$albums = new Albums();
$this->view->albums = $albums->fetchAll();
}
public function addAction()
{
$this->view->title = "Add New Album";
$form = new AlbumForm();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$albums = new Albums();
$row = $albums->createRow();
$row->artist = $form->getValue('artist');
$row->title = $form->getValue('title');
$row->save();
$this->_redirect('/');
} else {
$form->populate($formData);
}
}
}
public function editAction()
{
$this->view->title = "Edit Album";

$form = new AlbumForm();
$form->submit->setLabel('Save');
$this->view->form = $form;
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$albums = new Albums();
$id = (int)$form->getValue('id');
$row = $albums->fetchRow('id='.$id);
$row->artist = $form->getValue('artist');
$row->title = $form->getValue('title');
$row->save();
$this->_redirect('/');
} else {
$form->populate($formData);
}
} else {
// album id is expected in $params['id']
$id = (int)$this->_request->getParam('id', 0);
if ($id > 0) {
$albums = new Albums();
$album = $albums->fetchRow('id='.$id);
$form->populate($album->toArray());
}
}
}
public function deleteAction()
{
$this->view->title = "Delete Album";
if ($this->_request->isPost()) {
$id = (int)$this->_request->getPost('id');
$del = $this->_request->getPost('del');
if ($del == 'Yes' && $id > 0) {
$albums = new Albums();
$where = 'id = ' . $id;
$albums->delete($where);
}
$this->_redirect('/');
} else {
$id = (int)$this->_request->getParam('id');
if ($id > 0) {
$albums = new Albums();
$this->view->album = $albums->fetchRow('id='.$id);
}
}
}
}

These code are from Rob Allen’s ZF tutorial. The link is http://akrabat.com/zend-framework-tutorial/

In conclusion, ZF framework is easy to use. The hard part is to manage the path of the library files and the directory structure. This problem is similar to Java’s Classpath issue. Once you get the Path issue resolved, the rest is pretty easy to maintain. The directory structure (auto generated by Zend IDE) is shown below.

Zend IDE’s Auto Generated ZF Directory Structure

Tool Talk – Looking for the right tool to build my website

I am trying to build my website – http://FriendsLink.Are4.us. I am envisioning this is a website that connects and engages friends across the world, whether it is for learning or refreshing the knowledge of a foreign (new) language, swapping items between friends and friends’ friends, or setting up a common interest group.

Last week, I asked a Hungarian software development company to submit their bid for writing the prototype. They quoted me 97,600 Euros (about $160K). This price is definitely a shocker. In their term sheet, they are quoting me 60 Euros (~$100) per hour for business analyst work. I am not going to pay for that kind of ridiculous expense. :-( :>

So, I have decided to write my own application. Right now, I am using BlueHost as my web hosting company. And, they only support either PHP or Ruby on Rails. I am a fluent Java and Dot Net developer. And, I know my way around Java and C# technology. A few years ago, I had an online computer journal – P2P Journal (http://p2pjournal.are4.us). And, that was my last brush with PHP programming language. Because the business climate for P2P technology in USA is not so good, I decided to consolidate all my websites together to reduce cost.

Now, I must refresh my PHP knowledge. I downloaded Zend Studio. The Zend Studio is based on the IBM’s Eclipse framework, which is the most popular Java development IDE. Although I personally prefer NetBeans over the Eclipse as a development tool, I must say that Zend PHP Studio is a nice tool and really facilitates application development. Here is a screen shot of the IDE.

Zend Studio IDE

I also investigate the Zend Framwork. Here is a nice blog that gives a 10,000 feet overview of the Zend Framwork. And, Rob Allen has written an excellent tutorial about the 1.5.2 framework. The other tutorial on IBM developer website is out-of-date and of little use, because it talks about 0.12 version of the framework.

Zend Framework reminds the Struts framework for Java development, particularly the Front-Controller pattern, which is quite similar to the ActionController in Struts framework.

Additionally, I have started using RapidWeaver. This is a nifty little website authoring tool. It provides several nice looking templates for writing web pages. RapidWeaver is geared toward non-programmers and allows users to use click-and-drop to quick develop the website. On the other hand, Adobe (Macromedia) DreamWeaver is much geared toward the programmers. So, my take for RapidWeaver is that is a fast website authoring tool. Its weakness is limited programmer support, e.g. it provides very little support for writing PHP codes. Below is a screen shot of my future website using RapidWeaver.

RapidWeaver Screenshot

Visiting BikeMart

This Saturday, I visited the BikeMart in Richardson, TX. Apparently, there is an active bicycling community right here in DFW area. If you watch Tour de France, you know that Lance Armstrong was the Champion of the Tour de France from 1999 – 2005. And, he is a Richardson native. Now, he lives in Austin, TX.

When I was in the BikeMart, I talked with some employees. And, they told me the story between Jim Hoyt (BikeMart owner) and Lance Armstrong. About 10 – 15 years ago, when Lance was getting ready to become a professional bi-cycling athlete, he did not have a lot of money. And, the owner of BikeMart loaned Lance a good bike for racing. After Lance won the Tour de France, he gave Jim many souvenirs for display. On the wall, inside the store, there are many display of Lance’s Yellow Jerseys as well as several photos from the tour.

A good bike is quite expensive, some costs in $6,000 to $9,000. However, those bicycles are really light. I even weighted a $6,000 bike. It is only 15 pounds, about 6 1/2 kilos. It is made of space-age materials, e.g. Titanium, and is exceedingly strong. I also saw a mountain bike with liquid suspension system.

It is really a cool experience to look and feel (touch) one of those fancy bicycles. The store is in a shopping center at the corner of Campbell and Coit, in Richardson, TX. The website is http://bikemart.com

Week 1 – Starting iBarter Club

This week, I decided on starting a social bartering club – ibarter. This is an experiment for followings.

1. Whether it is better to exchange good and services in your friends’ social network rather than with a faceless merchant on the Internet? Is a system based on existing relationships more trustworthy than unknown relationships?
2. We all have seen “Help Wanted” ads. If we start a bulletin board which allows people posting both “Looking for Services & Goods” and “Offering Services & Goods” ads, will there be sufficient interest in this bi-directional website? Most people are consumers. Does people have motivation to act as providers of goods and services?
3. Can a bartering system work? Can we really depart from the money-based settlement financial system, which we are so accustomed to? Will people feel comfortable using a value system? We all know that currencies fluctuate in value; foreign exchange rate goes up and down, value of goods and services inflate or deflate over time. Will there be a better alternative to such a system. Will people be happier with a point based system, where the value of a point is judgement by everyone (people of the world), instead of being dictated by the government?

With these questions in minded, I am working with Peter, one of my long-time friends from the German Language school in Vienna. I am planning to do all the architectures and business use cases. And, Peter is helping me in locating suitable talents to help us implement this project. In a way, I am relying on my social network to get this job done rather than on a faceless, unknown contact.

We are planning to develop a web (bartering) portal that connects to some social networks, e.g. Facebook, iwiw, Myspace. And, our web portal will leverage off a person’s social relationship to help people exchange goods and services.

Stay tuned to our progress.

iPod Touch

This April, I bought an Apple iPod Touch, with 32 GB capacity from Amazon. I got it on sale and was $25 cheaper than the market price at CompUSA, Best Buy or even at the Apple Store. Buy it from Amazon. Shopping on the web is really cheaper than the brick-and-mortar stores!

This has been a great tool in helping me with learning foreign languages. I was able to download a variety of language training PODCASTs from the web for free. For example, I can listen to daily news from Deutsche-Welle in German language. And, I have also started learning Spanish. PODCAST lets you listen to audio whenever and wherever you want.

I am also planning to write some applications for my iPod Touch. So, I have downloaded the Apples’ iPod developer SDK.