Documentation
Installation
Downloading
Start by downloading Glue from either the tar or zip files. Decompress the files and move the glue.php file to your Document Root.
If you use mercurial, you may also clone the public repository using:
$ hg clone https://jtopjian@bitbucket.org/jtopjian/gluephp
Apache Configuration
Glue requires mod_rewrite for pretty-URLs. The following standard .htaccess file works well:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
Also make sure your site configuration has AllowOverride All set.
Hello, World!
The following example illustrates a simple “Hello, World!”. Copy and paste the below code into `index.php` and then access it. Ensure you have glue.php in the same directory.
<?php
require_once('glue.php');
$urls = array(
'/' => 'index'
);
class index {
function GET() {
echo "Hello, World!";
}
}
glue::stick($urls);
?>
URLs
URL Basics
The main component of Glue is the $urls array. This is an associative array where the key is the URL you want to match and the value is the Class to run when matched.
The following are all “static” URLs that are being mapped to specific PHP classes:
<?php
$urls = array(
'/' => 'index',
'/contact.html' => 'contact',
'/about.html' => 'about'
);
?>
Regular Expressions in URLs
You can also use Regular Expressions in your URL keys:
<?php
$urls = array(
'/' => 'index',
'/article/[a-zA-Z0-9]+.html' => 'article'
);
?>
The above example would match:
http://example.comhttp://example.com/article/HelloWorld.htmlhttp://example.com/article/abcdefg.html
Capturing Data in URLs
You can also capture parts of the URLs and pass them on to the class methods:
<?php
require_once('glue.php');
$urls = array(
'/' => 'index',
'/(\d+)' => 'index'
);
class index {
function GET($matches) {
if ($matches[1]) {
echo "The magic number is: " . $matches[1];
} else {
echo "You did not enter a number.";
}
}
}
glue::stick($urls);
?>
When you visit http://example.com you will see “You did not enter a number.”.
However, visiting http://example.com/500 will output “The magic number is 500”.
You are not restricted to using the variable name $matches. This can be any name you want. It will always contain an array of matched regular expressions from $urls.
Using Named Regular Expressions
Named Regular Expressions are a rather unknown regular expression feature. They allow you to “tag” or name a regular expression for later reference. By using them in Glue, you’re able to have an associative $matches array instead of a simple index-based array.
<?php
require_once('glue.php');
$urls = array(
'/' => 'index',
'/(?P\d+)' => 'index'
);
class index {
function GET($matches) {
if (array_key_exists('number', $matches)) {
echo "The magic number is: " . $matches['number'];
} else {
echo "You did not enter a number.";
}
}
}
glue::stick($urls);
?>
Class Methods
The second most important parts of Glue are the methods contained in each class. Each method corresponds to the type of HTTP Method requested. The majority of the time, these will be GET methods.
When a web page is requested, the browser issues GET. When submitting a form, the browser will issue POST with the submitted form data. There are a few other HTTP methods, but not used nearly as often as GET or POST.
This example shows how to use GET and POST to process a form:
<?php
require_once('glue.php');
$urls = array(
'/' => 'index'
);
class index {
function GET() {
echo '';
}
function POST() {
echo 'The value you entered was ' . $_POST['textbox1'];
}
}
?>
The Static Method
The final component of Glue is the glue::stick() Static Method. It takes one argument: the $urls array.
<?php
glue::stick($urls);
?>
glue::stick’s job is to process the requested URL with your $url’s and run a matching class if one exists. If a matching URL does not exist, Glue will throw an error.