Posted (Updated ) in PHP

In Kohana 2, html::script() and html::stylesheet() accepted an array or string as their first arguments. This allowed you to easily add multiple scripts and styles simultaneously without needing to write several lines of redundant code. Kohana 3 has removed this useful ability so I thought I’d write a very quick extension to add the feature back in. The following script adds HTML::scripts() and HTML::styles() (As Kohana 3 slightly renamed their functions to HTML::script() and HTML::style() respectively).

Simply add the following file html.php to your /classes directory:

<?php defined('SYSPATH') or die('No direct script access.');
 
class HTML extends Kohana_HTML {
	public static function scripts(array $scripts, $attributes=array(), $index = FALSE)
	{
		$response = '';
 
		//Data sanitisation
		$index = $index ? TRUE : false;
		if ( !is_array($attributes) ) $attributes = array();
 
		foreach ( $scripts as $script )
			$response .= html::script($script, $attributes, $index);
 
		return $response;
	}
 
	public static function styles(array $styles, $attributes=array(), $index = FALSE)
	{
		$response = '';
 
		//Data sanitisation
		$index = $index ? TRUE : false;
		if ( !is_array($attributes) ) $attributes = array();
 
		foreach ( $styles as $style )
			$response .= html::style($style, $attributes, $index);
 
		return $response;
	}
}

Download: html.php

Usage:

<?php
echo HTML::styles(array(
	'assets/css/reset',
	'assets/css/styles',
);
echo HTML::scripts(array(
	'assets/js/jquery',
	'assets/js/jquery.ui.all',
);
?>

Note: Both functions accept the latter 2 parameters available in Kohana 3 HTML::scripts() and HTML::styles(). If used, these arguments will be applied to every element in the first arguments array.

Read More »

Posted (Updated ) in Database, PHP

It’s probably become apparent by now that I like Doctrine. It’s quick, it’s easy and it works with everything. So here’s a Doctrine driver for the Kohana Auth module, allowing you to use Doctrine instead of Kohanas integrated ORM.

Download: Kohana Auth Doctrine Driver 1.03 here

Installation instructions:
Unzip the module to your /modules folder.
Enable the module by adding

'auth_doctrine' => MODPATH.'auth_doctrine',  // Doctrine driver for Auth module

to the modules array in the following file:
Kohana 3:/bootstrap.php
Kohana 2: /config/config.php

UPDATE Jul 15 2010: Fixed incorrect user login bug. Please redownload
UPDATE Dec 22 2010: Fixed ‘remember’ session bug.
UPDATE Jan 3 2011: Fixed bug if calling Auth::instance()->logged_in(‘role_name’). Thanks to freenode #doctrine user SirFunk for this one – good catch!
Update Feb 21 2011: New module at a new home! See here for details

Read More »

Posted (Updated ) in Database, PHP

Update Feb 21 2011: New module at a new home! See here for details

Upon the release of Kohana 3, one of the first things I wanted to do was install Doctrine. The following is a short tutorial on how to do; and keeping with Kohanas modular style, it will be placed in its own reusable module. Time to begin.

Create the following files/folder:
/modules/doctrine
/modules/doctrine/init.php
/modules/doctrine/classes
/modules/doctrine/classes/doctrine

Inside /modules/doctrine/classes/doctrine drop the official latest build (see here) of Doctrine such that Doctrines CHANGELOG, COPYRIGHT etc files etc are inside.

Enter the following into init.php. Note – this may not be the optimal bootstrap file – feel free to tweak to your hearts content.

Read More »

Posted (Updated ) in Database, PHP

When it comes to online shopping carts, LemonStand is the hot new thing. Still in beta, this piece of software boasts a surprisingly large feature set and high level of customization options usually only seen in much larger CMS’s. The one narking issue I’ve had with it so far is its choice of URL separator – preferring underscores over dashes. After speaking to the very polite and friendly developer behind LemonStand, a solution was quickly devised.

To convert the default URL separator in LemonStand from underscores to dashes simply add the following line of code to your config/config.php file.

$CONFIG['URL_SEPARATOR'] = '-';

Update 3 Jun 2010: I’ve received word from the author behind LemonStand that he’ll be publishing an update later today changing the default URL separator from underscores to dashes. The change will not affect any existing installations and only apply to new installations. For developers wishing to change the default separator on new pages on existing installations, the fix above still applies. This is great news that I’m sure many developers out there will be pleased with.

Read More »

Posted (Updated ) in Javascript, PHP

Earlier tonight I wanted to remove the ancient version of JQuery automatically loaded on the WordPress front end and replace it with the much speedier latest version (currently 1.4.2). This turned out to be more difficult than planned, however after a bit of scrounging I found the solution; add the following code to your template’s functions.php file:

if( !is_admin() )
{
	wp_deregister_script('jquery');
	//Add latest JQuery back into header. Comment this line out to remove JQuery alltogether
	wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '1.4.2');
	wp_enqueue_script('jquery');
}

Enjoy your blazingly fast JavaScript experience 🙂

Read More »

Posted (Updated ) in Javascript, PHP

UPDATE 15 Feb 2012: This post is woefully out of date. See my new PHP WebSocket Chat Application 2.0 post for a working tutorial compatible with the latest WebSocket spec as of Feb 15, 2012.

Today I’ll be doing an in-depth tutorial on how to create a simple, real-time chat application with HTML5 web sockets.

Disclaimer: Due to the relative infancy of web socket technology, this tutorial will currently only work on Google Chrome and the spec isn’t finalized yet so it may break in the future (specifically during the handshake phase).

UPDATE Nov 14, 2010: Since the writing of this post, the handshake spec has changed in such a way that browsers requiring the new spec will return the error code INVALID_STATE_ERR upon connection. I checked the PHPWebSocket page for an updated version but it looks like the developer hasn’t released one yet so instead I now recommend using Bohuco’s excellent websocket script instead.

Read More »

Posted (Updated ) in Linux, PHP

This tutorial will show *nix users how to get a nice notification displaying on their screen when people visit their site using a PHP and a Python script. This isn’t practical for sites with large numbers of visitors but it’s great for people running small sites on their local machines.

Read More »

Posted (Updated ) in PHP

Out of the box, CodeIgniter only supports MVC. This tutorial explains how to add HMVC to CodeIgniter by installing BackendPro 0.6.1 into a Multi-Site CodeIgniter 1.7.2 installation for some nice, modular goodness.

Read More »

Posted (Updated ) in Database, PHP

WordPress is great on its own but if you want to do alot of database work with it you’ll quickly realize how frustratingly limited the wpdb class is. In this tutorial I’ll show how to integrate Doctrine into WordPress through the use of a plugin and make your developing life so much easier.

Read More »

Posted (Updated ) in PHP

Including every file in a specific directory automatically is a useful way of auto-loading classes in PHP. This could be done manually of course but we’re developers and we’re lazy so here’s a better solution utilizing PHP’s glob() function:

<?php
	foreach( glob(dirname(__FILE__) . '/classes/*.php') as $class_path )
		require_once( $class_path );

In this script, glob() searches for all pathnames matching the given pattern (In this case all files with a .php extension in the ‘classes/’ directory). Please note that if glob() doesn’t find any matches it will return false instead of the expected array, so unless you’re iterating over its results with a function that checks for this – such as foreach – either typecast or perform some other form of validation before use. You’ve been warned.

UPDATE 15 May 2010: Found this informative article related to the various ways of loading all files in a directory and their respective speeds – performance matters, people!.

Read More »