A common practice across most PHP frameworks is that of routes. A routing class tells your application how to handle URL's and what method to call, class to create, etc.

PHP Rest Routing

Routing classes are typically used in conjunction to an MVC layout (Model-View-Controller) and it is responsible for mapping URL's to their corresponding Controller and delegate methods. You can use different HTTP methods to fully benefit from using REST / resourceful routes. This also allows you to create SEO friendly URL's that can also pass dynamic parameters to their corresponding Controller.

The main features typically are:

  • REST Routing
  • Use different HTTP methods for same URL’s to determine different types of action / request
  • Support dynamic URL segments
  • Provide the capability for reverse routing; creating the destination URL from an array of route variables
  • SEO friendly URL's using a .htaccess to route all URL's into your class

These classes are usually simple and lightweight and act as a fundamental point of any application. Below is some code put together to demonstrate how to create a routing class.

AltoRouter

AltoRouter was developed by Danny van Kooten and is an adaptation of an earlier PHP Routing Class. It is a tiny routing class which will help you in your future PHP projects providing the following features:

  • Usage of different HTTP Methods (GET, POST, PUT, DELETE)
  • Dynamic routing, which lets you capture certain uri segments as parameters
  • REST Routing
  • Reversed routing, generate URL’s from route names while supplying parameter values
  • Wildcards
  • Parameter types
# Create our class
$router = new AltoRouter();
$router->setBasePath('/AltoRouter');
# Paths to map
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
# Generate the URL
$router->generate('users_show', array('id' => 5));

For full documentation, visit his GitHub account - AltoRouter

Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. Slim is easy to use for both beginners and professionals. Slim favors cleanliness over terseness and common cases over edge cases. Its interface is simple, intuitive, and extensively documented - both online and in the code itself.

We previously looked at Slim when we discussed how to Implement Your Own REST API. Once again, Slim is a fantastic option providing you with a lot of freedom and features:

  • Powerful router
    • Standard and custom HTTP methods
    • Route parameters with wildcards and conditions
    • Route redirect, halt, and pass
    • Route middleware
  • Template rendering with custom views
  • Flash messages
  • Secure cookies with AES-256 encryption
  • HTTP caching
  • Logging with custom log writers
  • Error handling and debugging
  • Middleware and hook architecture
  • Simple configuration
# Create the instance
$app = new SlimSlim();
# Define a GET method
$app->get('/api/election/:ID', function($ID) {
    echo "Election: $ID";
});
# Define a POST method
$app->post('/api/election/', function() {
    echo 'Check POST for errors, etc..';
});
# Define a PUT method
$app->put('/api/election/:ID', function($ID) {
    echo 'Update election identified by $ID';
});
# Define a DELETE method
$app->delete('/api/election/:ID', function($ID) {
    echo 'Delete the election identified by $ID';
});
# Run the application 
$app->run();

Other Resources

We have only touched on a small selection of options available. Below is a list of other MVC setups you may wish to consider and articles detailing further PHP routing classes:

Do you use a PHP router class or have you tried any of the classes we listed above? Leave a comment below and let us know!

15
Ignite your brand, utilise user-generated content no matter where you or your audience are ›