Original URL: http://www.theregister.co.uk/2008/09/24/php_on_rails/

Get your PHP on the right Trax

Framework flattery

By Deepak Vohra

Posted in Developer, 24th September 2008 15:02 GMT

Hands on Ruby on Rails has become a popular framework for developing database-based web applications using the Model-View-Controller (MVC) pattern.

Before Ruby on Rails, though, PHP was hogging the web-development limelight. Problem was, there was no Model-View-Controller (MVC) framework for PHP.

With Ruby on Rails, though, PHP developers have come to realize the timesaving benefits of MVC - a fact that led to the development of various PHP frameworks that are actually based on Ruby on Rails.

Among them, the Akelos framework and PHP On Trax.

In this article we shall develop an MVC Create-Read-Update-Delete (CRUD) application using the PHP On Trax Framework. Why this particular framework? Simple: it is a direct port of the Ruby on Rails framework.

Install PHP On Trax

Install Apache HTTP Server 2.2 in C:/Apache directory and PHP 5.2 in C:/PHP directory, and configure the Apache server with PHP. Install a MySQL 5.0/6.0 database and enable the MySQL extension in the php.ini configuration file. by removing the ';' from the following line:


Set the extension_dir directive in php.ini to the directory containing the extensions.

extension_dir = "./ext"

Set error reporting in php.ini file to E_ERROR.

error_reporting  =  E_ERROR

We also need to install the MDB2 driver for MySQL, which is available as a PHP Extension and Application Repository (PEAR) module. First, install PEAR if not already installed. Download go-pear.php and run the following command to install PEAR:

C:/PHP>PHP go-pear.php

Download the MDB2 driver for MySQL Copy the .tar file to the C:/PHP directory. Install the MDB2 driver with the following command:

C:/PHP>pear install -o MDB2_Driver_mysql-1.4.1.tar

PHP On Trax is available as a PEAR module. Run the following commands to install PHP On Trax:

>pear channel-discover pear.phpontrax.com
>pear install trax/PHPonTrax

A PHP on Trax directory gets created in the C:\PHP\PEAR directory. Create a trax.bat file in the C:/PHP directory, which is in the PATH environment variable. To the trax.bat file add the following code:

php C:\PHP\PEAR\PHPonTrax\trax.php %1

Add the .phtml type to the httpd.conf file to render the .phtml view templates.

AddType application/x-httpd-php .php .phtml

The .phtml view templates contain a short form of PHP's open tags. Enable the short open tags in php.ini file.

short_open_tag = On

Start your build

In this section we create a PHP on Trax application. The MVC application shall be used to create, read, update and delete catalog entries in a database table. Create an application catalog in the Apache web server root using trax.bat with the following command:

C:\Apache\htdocs >trax catalog

A trax application with an application structure similar to a Ruby on Rails application gets created. The app directory consists of the controllers directory for the controller PHP scripts, the models directory for the model scripts and the views directory for the view templates.

A database.ini database configuration file gets created in the config directory. Modify the database.ini file for the MySQL database. We shall be using the development mode as follows:

    phptype = mysql
    database = test
    hostspec = localhost
    username = root
    password = 
  persistent = true

Modify the config/environment.php directory to set the PHP directory, the trax root directory, and the trax environment mode. Define variables PHP_LIB_ROOT, TRAX_ROOT, and TRAX_ENV.

define("PHP_LIB_ROOT",    "C:/PHP/PEAR");
define("TRAX_ROOT",       "C:/Apache/htdocs/catalog");
define("TRAX_ENV",    "development");

The public/.htaccess file specifies configuration directives for the Apache HTTP server. The configuration directives in the .htaccess file apply to the directory in which the .htaccess file is placed and the sub-directories of the directory. Modify the public/.htaccess file. Replace the following line with the subsequent line:

php_value include_path .:C:\Apache\htdocs\catalog/config
php_value include_path .;C:\Apache\htdocs\catalog/config

The include_path directive specifies a list of directories and is used to locate files. Access the Trax application console with the URL http://localhost/catalog/public.

Modify the C:\Apache\conf\httpd.conf file so the directives in the .htaccess file may override earlier access information. Activate the mod_rewrite module by removing the '#' before the following line:

LoadModule rewrite_module modules/mod_rewrite.so

The mod_rewrite module provides a rules-based rewriting engine to rewrite requested URLs. Also set all the AllowOverride directives to All.

AllowOverride All

The AllowOverride directive specifies which directives in the .htaccess may override earlier access information. Modify the DocumentRoot directive and <Directory> setting in httpd.conf to the following:

DocumentRoot "C:/Apache/htdocs/catalog/public"
<Directory "C:/Apache/htdocs/catalog/public">

Reboot time

Restart the Apache server. We shall generate a model class and the scaffolding for the model class, which models a database table, with the scaffold generator. Create a scaffold.bat file in the C:\Apache\htdocs\catalog directory. Copy the following code to the scaffold.bat file:

php ./script/generate.php scaffold  %1 %2

Before creating the scaffolding, build a database table catalogs in the MySQL database with the SQL script catalog.sql. The primary key field should be "id" and of type INT. Create a scaffolding for the "catalogs" table with the following command:

C:\Apache\htdocs\catalog>scaffold catalog catalog

A model class catalog.php gets generated in the models directory.

class Catalog extends ActiveRecord {      

A controller script catalog_controller.php gets created in the controllers directory. View templates _form.phtml, edit.phtml, index.phtml, add.phtml, and show.phtml get created in the views directory.

Modify the _form.phtml view template. Add the following line as the first line in the _form.phtml form:

<p><label for="catalog_id">Id:</label><br/>
<?= text_field("catalog", "id") ?></p>

The default URL for a URL action is http://localhost/controller/action. Custom URLs may also be defined in the config/routes.php file. For example, the add controller action of catalog controller may be invoked by specifying a custom router in the config/routes.php file. With the custom router the url http://localhost/catalog invokes the add action of the catalog controller.

$router->connect( "catalog",array(":controller" => "catalog",":action" => "add") );

Next, we shall use the scaffolding for the catalogs table to add a new catalog entry. Invoke the index controller action with the URL http://localhost/catalog/index. The catalogs listing gets displayed. The Show, Edit, and Delete links are for displaying, editing and deleting a catalog entry respectively. To create a new listing click on the New hyperlink.

In the New catalog view template, create a new catalog entry and click on Create.

A new catalog entry gets added to the database table catalogs and gets listed in the "Listing catalogs".

There you have it - that's how you create a PHP MVC CRUD application without using too much PHP code thanks to the PHP On Trax Framework. You can download code for the project here