PHRoute is an interesting package: it’s a fast regular expression based router that you can easily implement in small to medium projects. However, it’s not just very fast: there are filters, filter groups and named routes. You can also use a basic controllers system if things are getting bigger.
That said, today we will see how to use it and how to implement its features in a sample project. Also, we are going to see what’s under the hood: PHRoute is a result of many experiments and tests by different people.
Let’s start by installing it!
Install
You can add PHRoute to your project with Composer in seconds. Just add this line to your composer.json
file:
{
"require":
{
"phroute/phroute": "1.*"
}
}
Type the composer install
command and you’re in. Now, let’s move on to our test project.
Sample Project & First Example
For a better understanding of every concept of PHRoute, it is a good idea to have a sample project to work with. Today we are going to make a basic API for a books database service.
Here’s the database scheme we are going to use:
If you want to do some tests, this is the SQL schema dump I used (with some extra dummy data).
CREATE TABLE IF NOT EXISTS authors (id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(250) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
INSERT INTO authors (id, name)
VALUES
(1, 'Dan Brown'),
(2, 'Paulo Coelho');
CREATE TABLE IF NOT EXISTS categories (id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(250) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
INSERT INTO categories (id, name)
VALUES
(1, 'Thriller'),
(2, 'Novel');
CREATE TABLE IF NOT EXISTS books (id int(10) unsigned NOT NULL AUTO_INCREMENT, title varchar(250) NOT NULL, isbn varchar(50) NOT NULL, year int(11) NOT NULL, pages int(11) NOT NULL, author_id int(10) unsigned NOT NULL, category_id int(10) unsigned NOT NULL, PRIMARY KEY (id), KEY author_id (author_id,category_id), KEY category_id (category_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7;
INSERT INTO books (id, title, isbn, year, pages, author_id, category_id)
VALUES
(1, 'The Zahir', '0-06-083281-9', 2005, 336, 2, 2),
(2, 'The Devil and Miss Prym', '0-00-711605-5', 2000, 205, 2, 2),
(3, 'The Alchemist', '0-06-250217-4', 1988, 163, 2, 2),
(4, 'Inferno', '978-0-385-53785-8', 2013, 480, 1, 1),
(5, 'The Da Vinci Code', '0-385-50420-9', 2003, 454, 1, 1),
(6, 'Angels & Demons', '0-671-02735-2', 2000, 616, 1, 1);
We are not going to write anything really complex. Actually, writing some routes to emulate an API request in a very basic way will be enough. If you want to write a real world API there are many concepts you have to know, but today we are just taking a look at PHRoute.
Before we start with specific routes, let’s analyze the main application structure. This is what we are going to put in ourindex.php
file.
<?php
require 'vendor/autoload.php';
function processInput($uri){
$uri = implode('/',
array_slice(
explode('/', $_SERVER['REQUEST_URI']), 3));
return $uri;
}
function processOutput($response){
echo json_encode($response);
Truncated by Planet PHP, read more at the original (another 5133 bytes)
more
{ 0 comments... » Fast PHP Routing with PHRoute read them below or add one }
Post a Comment