Subversion hosting, CVS hosting, Trac hosting, Bugzilla hosting and software collaboration Providing hosted Subversion, CVS, Trac and Bugzilla repositories

March 7, 2008

Basic Views In CodeIgniter

Filed under: Software Development — Tags: , , , , — Greg Larkin @ 3:24 pm

Hi everyone,

A while back, I hooked up my CodeIgniter sample application to a MySQL database. That was easy, and now I’ll show how to move your presentation code into separate view files, instead of echo’ing HTML from inside a controller file.

According to the CodeIgniter manual, views are stored in the /views (hmm, that makes sense!) directory of your application, and they can be divided up across functional areas with subdirectories. Since it’s possible to create page snippets and load them in sequence, you might have a directory structure like this:


Then when you want to render a particular page, say the “add user” page of your web app’s administrative interface, the code would look like this:


I haven’t gotten that complex yet, but I did move the code to display my user count into a view and set the value in the controller like so:

function count_users()
        $numUsers = $this->user->count_users();
        $data = array(‘num_users’ => $numUsers);
        $this->load->view(‘user/showcount’, $data);

The file /views/user/showcount.php is pretty simple:

<head><title>Welcome to the User Area!</title></head>
This page is rendered by the file: <?php echo __FILE__; ?>
<p>There are <?php echo $num_users; ?> users defined in the database.
<p><?php echo anchor(‘user’, ‘Go back’); ?>

Ok, this all seems to be pretty easy. Of course, there’s a bunch more functionality in the CodeIgniter framework, but I think what I’ll do for my next post is shift gears and go through the same basic exercises using CakePHP, Prado, and the Zend Framework.

It will be interesting to see if there’s any similarity to the way the classic “Hello, world.” program varies in complexity based on implementation language!

Call me - Greg Larkin: error

March 6, 2008

FreeBSD Port For CodeIgniter Upgraded to 1.6.1

Filed under: Software Development — Tags: , , , , , — Greg Larkin @ 7:01 pm

Hi everyone,

I’ve submitted some new FreeBSD port upgrades over the past week, including new support for CodeIgniter 1.6.1. In addition to the version bump of the upstream distribution, I also added some new bits to allow customization of certain CI files. The port is also careful to avoid removing those modified files when a new port upgrade comes along.

The reference for how to handle user-configurable files installed as part of a port can be found in the excellent FreeBSD Porter’s Handbook in the Configuration Files section.

In the new CodeIgniter port, the following user-configurable files are installed:


What this does is install a reference copy of each file (*.sample) that is not expected to be edited by the user. The actual file that CodeIgniter uses to render pages (no .sample suffix) is user-configurable as needed.

Then some crazy-looking shell code in the pkg-plist file makes sure that any edited files are not removed if the port is deinstalled or upgraded (reformatted for readability):

@unexec if cmp -s %D/%%WWWDIR%%/%%CI_CONF_DIR%%/autoload.php.sample \

    %D/%%WWWDIR%/%%CI_CONF_DIR%%/autoload.php; then \

    rm -f %D/%%WWWDIR%%/%%CI_CONF_DIR%%/autoload.php; else \

    %%ECHO_MSG%% "===> Customized %D/%%WWWDIR%%/%%CI_CONF_DIR%%/autoload.php \

    has not been removed"; fi


@exec if [ ! -f %D/%%WWWDIR%%/%%CI_CONF_DIR%%/autoload.php ]; then \

    cp -p %D/%F %B/autoload.php; fi

This concept needs to be implemented in several of the other ports that I maintain, including CakePHP and Prado. If anyone has a list of files that are user-configurable in each of those frameworks, please send it along!

Call me - Greg Larkin: error

February 20, 2008

Hooking My CodeIgniter Application to MySQL

Filed under: Software Development — Tags: , , , — Greg Larkin @ 7:19 pm

Hi everyone,

When I last posted about my sample application development with CodeIgniter, I had created a simple Hello, World application. The next thing I want to explore is how easy it is to connect to a MySQL database and retrieve data.

As a quick side note, the CodeIgniter User Guide is well-written and very clear with plenty of examples. That’s always nice to see and instills some confidence in the quality of the underlying code.

According to the guide, all of the database connections should be defined in the application/config/database.php file. After setting up a new database named “ci” and a database user, I opened database.php and set up my connection:

$db[‘default’][‘hostname’] = "localhost";
$db[‘default’][‘username’] = "ci";
$db[‘default’][‘password’] = "ci";
$db[‘default’][‘database’] = "ci";
$db[‘default’][‘dbdriver’] = "mysql";
$db[‘default’][‘dbprefix’] = "";
$db[‘default’][‘active_r’] = TRUE;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = "";

That was easy enough. Now I’ve got the skeleton of a model defined in application/models/user_model.php:

class Group_model extends Model {
    function Group_model()

The $this-&gt;load-&gt;database() statement automatically connects to MySQL using the default connection I defined previously. Now I can use the $this-&gt;db object to run queries and process results elsewhere in my model file.

I’ll replace my original hard-coded count_users function with something that runs an actual database query:

function count_users()
    $query = $this->db->query(‘SELECT COUNT(*) AS num_users FROM user’);
    if ($query->num_rows() > 0)
        return $query->row()->num_users;

Of course, I have to leave a newbie coding error in there for someone to notice. Anyone? Anyone? Bueller?

Here’s my simple MySQL database schema:

– Table structure for table `user`

`user_name` VARCHAR(128) NOT NULL,
`full_name` VARCHAR(128) NOT NULL,
`email` VARCHAR(128) NOT NULL,

– Dumping data for table `user`

INSERT INTO `user` (`id`, `user_name`, `full_name`, `email`) VALUES
(1, ‘glarkin’, ‘Greg Larkin’, ‘’),
(2, ‘morsel’, ‘Morsel The Cat’, ‘’);

When I navigate to my /codeigniter/user/count_users/ URL, I see:

There are 2 users defined

Great! Next up, we’ll take a look at creating an actual output page with views.

Call me - Greg Larkin: error

February 15, 2008

CodeIgniter 1.6.1 Is Released

Filed under: Software Development — Tags: , , , , — Greg Larkin @ 4:31 pm

Hi everyone,

I just saw that CodeIgniter 1.6.1 has been released. I guess that means I’d better get on the stick and upgrade the FreeBSD CodeIgniter port! Once I do that, I’ll upgrade my PHP Framework VM with the new version of the port and continue on with the experimentation.

Call me - Greg Larkin: error

February 8, 2008

Diving Into CodeIgniter

Filed under: Software Development — Tags: , , , — Greg Larkin @ 5:19 pm

Hi everyone,

Now that I’ve got my FreeBSD virtual machine set up with the various PHP frameworks, it’s time to start putting together a sample application in each one to learn their strengths and weaknesses. The sample application will be a simple mailing list manager and contain common features such as:

  • User account self-registration
  • Email generation
  • Management interface
  • MySQL database backend

I think that will exercise enough of each framework to get a good handle on their functionality and ease of use.
I’m starting off with CodeIgniter. At first glance, the documentation seems excellent, and the code is commented very clearly. Looking at the index.php file in the root installation directory (/usr/local/www/codeigniter on FreeBSD), I see:

| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
$application_folder = "application";

Ok, that seems easy enough. To keep the default application intact and start working on my custom application, I simply copied the existing application directory to /usr/local/www/ci_sourcehosting and updated the index.php file.

$application_folder = "/usr/local/www/ci_sourcehosting";

According to the welcome page displayed by CodeIgniter, I have to change the views/welcome_message.php file to change the default page contents. Ok, easy enough…

Custom Hello World on CodeIgniter

The controllers/welcome.php controller is called by default, and that’s configured in the config/routes.php file in your application directory.

To create a new controller, simply create a new class that extends Controller and name it with a capital letter. Save the new class to the lowercase version of the class name into the /controllers directory, and you’re ready to test.

My simple class is:

class User extends Controller {
    function index()
        echo ‘Welcome to the user controller’;
    function count-users()
        echo ‘There are 0 users defined’;

When I visit, I see:

Welcome to the user controller

and displays:

There are 0 users defined

Ok, that’s pretty straightforward, but I did have to do a bit of mucking about to get the URLs to look like instead of<strong>index.php</strong>/codeigniter/user/count.

The CodeIgniter manual specifies a .htaccess file to be placed in the directory where the index.php front controller is located:

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Normally, this works fine, except Apache 2.2 specifies AllowOverride None by default, so .htaccess files don’t work. After changing that, I had to make one small change to the .htaccess file, since my CodeIgniter installation is rooted at /codeigniter:

RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]

Next time, we’ll take a look at hooking CodeIgniter to MySQL.

Call me - Greg Larkin: error

February 1, 2008

Installing PHP Frameworks with the FreeBSD Ports Tree

Filed under: Software Development — Tags: , , , , — Greg Larkin @ 11:21 am

Hi everyone,

Ok, let’s proceed to install the various PHP frameworks that we’ll use to create a sample application.

As I mentioned in a previous post, the basic command for installing any port in the tree is:

cd /usr/ports/&lt;category&gt;/&lt;appname&gt; &amp;&amp; make install clean

Let’s start with the CakePHP framework:
cd /usr/ports/www/cakephp &amp;&amp; make install clean

After typing that command, you should see this on the screen:

FreeBSD Port Installation Options

Hmm, what is that screen for? The FreeBSD ports collection has a robust infrastructure for configuring software applications prior to installation. In this case, CakePHP works with multiple different database backends, so the port writer (i.e. me) decided to provide options so the prerequisite bits are installed before the port proper.

Looking at the options provided here, the first one named “PROD” determines the way that the Apache web server is configured for CakePHP. If this is a production server, this option should be selected so navigating to “http://<servername>/” displays the CakePHP welcome page. Since I am installing multiple frameworks on the same machine, I’ll leave this option unchecked. That way, each framework welcome page will be found at “http://<servername>/<frameworkname>/”.

I plan to use MySQL as a database backend, so the following screenshot shows the MYSQL option selected. The dialog box is navigated with the tab and arrow keys on your keyboard, and the spacebar toggles the option selections:

FreeBSD Port MySQL Option

Tab to the OK button and hit Enter, and the port build starts. Here is the full transcript of the process:

CakePHP Port Build Transcript

Notice that by selecting the MYSQL option prior to installation, the port build fetched and built the MySQL client library and the PHP MySQL module, as well as some other packages:


This helps us get a working PHP installation that supports CakePHP out of the box.

Now, it’s the moment of truth – does the CakePHP default page display correctly? In order to load the page on your host machine, find the VM’s IP address with the following command:

/sbin/ifconfig -a | grep -w inet | grep -v | awk '{ print $2 }'

That gives me, so here’s the URL where I should find the CakePHP default page: The address will likely be different for you.

This looks good!

CakePHP Default Page

Now as an exercise for the reader, try installing the rest of the frameworks and make sure that they display their welcome pages:

Zend Framework

If you have any trouble, write in with comments and feedback.

Call me - Greg Larkin: error

January 18, 2008

FreeBSD 6.2 VMware Image for the PHP Framework Face-Off

Filed under: Software Development — Tags: , , , , , , — Greg Larkin @ 9:30 pm

Hi all,

After my last post, I realized it might be nice to provide a clean VMware image of FreeBSD 6.2 for folks who want to follow along as I try out the various PHP frameworks.

The first thing you’ll need to start up the virtual machine is one of the following free VMware tools:

Both of these tools allow you to run virtual machines on your server and/or desktop. The VMware Player is most useful if you just want to run pre-built virtual machines and use them for experimentation and evaluation. VMware Server gives the ability to create your own custom virtual machines and install your choice of operating system.

Once you have one of those tools installed on your machine, download a FreeBSD 6.2 VMware image from the BitTorrent tracker. You can find detailed instructions on setting up the VMware image in a subsequent blog posting.

The OS install is configured as follows:

  • Root user has no password
  • Networking is configured with DHCP and uses NAT for outbound connections
  • Extracted size is 11Gb
  • FreeBSD ports tree is installed in /usr/ports and is current as of 01/30/08
  • Latest security patches as of 01/17/08 have been applied

Extract the downloaded file somewhere on your drive and fire up VMware Player or VMware Server and follow the prompts to start the VM.

In case you’re new to FreeBSD, there’s a wealth of information in the Handbook. After you’ve logged in and had a look around, you can use the following command to shut the VM down cleanly:

/sbin/shutdown -p now

Look for a new post soon describing how to install each of the PHP frameworks.

Call me - Greg Larkin: error

January 15, 2008

PHP Framework Face-Off

Filed under: Software Development — Tags: , , , — Greg Larkin @ 3:22 pm

Hello everyone,

I recently created a FreeBSD port for each of the following PHP frameworks:

I originally started with the Zend Framework port a while back, thinking I would use it to rebuild the web site. Since I want all software on the FreeBSD servers to be installed and upgraded using the ports system, I had to build the ZF port first. The only problem with building a port is that it doesn’t always require you to learn the ins and outs of actually using the software that you are porting.

The web site rebuilding project is still in the planning phases, and now that I’ve created ports for some additional PHP frameworks, I thought it might be useful to put together a simple web site using each one. I’ve read a lot of comments about each one, the strong points and weaknesses, but I think a hands-on experiment with each one will help a lot to narrow down the final choice.

A slightly more advanced “Hello, world.” program than the one in my original dog-eared copy of K&R ought to make the final choice easy to make. I’ll post progress here, and send any comments you have along the way.

Call me - Greg Larkin: error

Powered by WordPress