Skip to content
PPRR - Pure PHP RegEx Router
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
CHANGELOG.md
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

PPRR: Pure PHP RegEx Router

A PHP Router using Regular Expressions (if needed) to parse the GET query string (or the rewritten URL) and callables to load pages.

PPRR needs PHP 7.1+ as it uses nullable typehint, return types and null coalescing operator.

Installation

Using Composer:

composer require aymdev/pprr

Getting started

Your links will look like example.com/?/route.

Simply create a new PPRR instance with an array as:

  • keys: a RegEx (without delimiter nor start/end anchors)
  • value: a callable

Example:

use Phunder\PPRR\PPRR;

new PPRR([
    '/route'        => [$obj, 'method'],                 # ?/route
    '/directory'    => [Class::class, 'staticMethod']    # ?/directory
]);

Sub-route prefixes

You can send a multidimensional array, the key for the nested route array will be used as a route prefix.

'/prefix' => [             # set the prefix
    '(?:/)?'     => ...    # ?/prefix  (prefix home page)
    '/something' => ...    # ?/prefix/something
    '/smtg_else' => ...    # ?/prefix/smtg_else
]

URL parameters

You can still use parameters in the URL, you just need to capture them and they will be sent to the callable of the matching route.

// Route: ?/item/122
'/item/(\d+)' => [$obj::class, 'methodWithArg']

No match ? Set a default route

You can send a callable after the routes array, it will be used as default route:

new PPRR([
    ...
], [Error::class, 'My404PageMethod']);

No need for RegEx ? Change the route parsing mode !

If your application is concerned by performance issues, you might want to avoid using unecessary regular expressions:

PPRR::setDefaultMode(PPRR::MODE_STRING);

But you still can use Regex for some routes:

// Start the route with "R>"
'R>/item/(\d+)' => [$obj::class, 'methodWithArg']

The contrary is possible:

PPRR::setDefaultMode(PPRR::MODE_REGEX);

new PPRR([
    'S>/item.list' => [$obj::class, 'noArgs']
]);

Note:

  • You must set the mode before instanciating a new PPRR router
  • Default mode is MODE_REGEX

Can't read your routes easily ? Set "Data Types" !

Data Types allow you to name Regex parts to make your routes easier to build and to read.

PPRR::setDataTypes([
    'ID'    => '(\d+)',
    'slash' => '(?:/)'
]);

You can insert Data Types keys in your routes between brackets.

new PPRR([
    '{slash}?'  => [Controller::class, 'Home'],
    '/foo'      => [
        '{slash}?'      => [Controller::class, 'Directory'],
        '/{ID}{slash}?' => [Controller::class, 'DirectoryItem']
    ]
]);

Note:

  • You must define them before instanciating a new PPRR router
  • This feature only works with route parsing mode MODE_REGEX
You can’t perform that action at this time.