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

July 16, 2009

Improving PHP Application Performance

Filed under: Software Development — Tags: , — Greg Larkin @ 9:47 am
Photo by: Mark McArdle

Photo by: Mark McArdle

Hi everyone,

I came across a helpful list of PHP optimization tips, and I’d like to share it with you here: http://php100.wordpress.com/2009/07/13/php-performance/

In addition, using the XDebug profiler for collecting performance data and KCacheGrind for viewing it is highly recommended to keep your PHP applications running smoothly!


Call me - Greg Larkin: error

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:

/views
  /global
    /layout
      /header.php
      /footer.php
  /navigation
      /left_side.php
      /bottom_links.php
  /user
    /add.php
    /edit.php
    /delete.php
    ...
  /group
    /add.php
    /edit.php
    /delete.php
    ...
  /...
  /...

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:

$this->load->view(‘global/layout/header.php’);
$this->load->view(‘global/navigation/left_side.php’);
$this->load->view(‘user/add.php’);
$this->load->view(‘global/navigation/bottom_links.php’);
$this->load->view(‘global/layout/footer.php’);

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:

<html>
<head><title>Welcome to the User Area!</title></head>
<body>
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’); ?>
</body>
</html>

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:


&lt;INSTALL_DIR&gt;/index.php
&lt;INSTALL_DIR&gt;/index.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/autoload.php
&lt;INSTALL_DIR&gt;/system/application/config/autoload.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/config.php
&lt;INSTALL_DIR&gt;/system/application/config/config.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/database.php
&lt;INSTALL_DIR&gt;/system/application/config/database.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/hooks.php
&lt;INSTALL_DIR&gt;/system/application/config/hooks.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/mimes.php
&lt;INSTALL_DIR&gt;/system/application/config/mimes.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/routes.php
&lt;INSTALL_DIR&gt;/system/application/config/routes.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/smileys.php
&lt;INSTALL_DIR&gt;/system/application/config/smileys.php.sample
&lt;INSTALL_DIR&gt;/system/application/config/user_agents.php
&lt;INSTALL_DIR&gt;/system/application/config/user_agents.php.sample

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

%%WWWDIR%%/%%CI_CONF_DIR%%/autoload.php.sample

@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()
    {
        parent::Model();
        $this->load->database();
    }
}

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`

CREATE TABLE IF NOT EXISTS `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(128) NOT NULL,
`full_name` VARCHAR(128) NOT NULL,
`email` VARCHAR(128) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


– Dumping data for table `user`

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

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

Telecommuting PHP Developer Position

Filed under: Jobs — Tags: , , — Greg Larkin @ 4:38 pm

Hi everyone,

SourceHosting.net recently started a professional services contract for a new client in Maine, and they  need a PHP developer to finish a project for a client of theirs. The system is a content management and e-commerce web site used to order and fulfill scientific supplies. The site is targeted to a typical LAMP stack.

I had a look at the database schema and the existing code, and it is surprisingly well-commented, indented nicely and follows a naming convention. That was a nice change of pace! I suspect that it won’t be too difficult to pick up what’s there and learn it well enough to finish the project.

This project is available to an off-site developer, and there is some time pressure to get it done. They would like a person who can work full-time over the next few weeks. If you are interested, please reply directly to me with an introduction, your resume and a description of recent LAMP projects. If that all looks good, I’ll put you in touch with the hiring manager.


Call me - Greg Larkin: error

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:

/*
|—————————————————————
| APPLICATION FOLDER NAME
|—————————————————————
|
| 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:
| http://www.codeigniter.com/user_guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
$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 http://192.168.95.128/codeigniter/user, I see:

Welcome to the user controller

and http://192.168.95.128/codeigniter/user/count-users 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 http://192.168.95.128/codeigniter/user/count instead of http://192.168.95.128/<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

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 SourceHosting.net 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 SourceHosting.net web site. Since I want all software on the SourceHosting.net 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