2017 © Pedro Peláez
 

library junction

image

codeeverything/junction

  • Friday, April 8, 2016
  • by codeeverything
  • Repository
  • 1 Watchers
  • 1 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Junction

A simple PHP router

Junction is a simple PHP router. Right now it's created as a personal project out of interest, but I plan to use it as the default router in my micro-RESTful PHP framework Planck., (*1)

Open source

Comments and suggestions welcome. This open source, so get forking and PRing if you want! :), (*2)

Tests

Junction comes with a ~~complete~~ set of tests., (*3)

You can run them with PHPUnit quite simply from your shell:, (*4)

vendor/bin/phpunit

Example usage

Simple route

Let's define a simple route, with only one path segment, (*5)

// define the route
$this->router->add('GET /hello', function () {
    // just return a string for the application to then work on
    return 'Hello, world';
});

// ... more routes ...

// handle the request, matching routes if possible
$response = $this->router->handleRequest();

The first argument to the add() funciton is a string which starts with the HTTP verb to match on, followed by the path requested., (*6)

Simple route with path variable

Junction allows you to simply define variables in your route, using a similar placeholder syntax to PDO queries on your DB, (*7)

// define the route - with required variable "name"
$this->router->add('GET /hello/:name', function ($name) {
    // just return a string for the application to then work on
    return 'Hello, ' . $name;
});

// ... more routes ...

// handle the request, matching routes if possible
$response = $this->router->handleRequest();

The variable "name" is passed in as the first argument in your handling function., (*8)

Simple route with optional path variable

// define the route - with optional variable "name"
$this->router->add('GET /hello/:name?', function ($name) {
    // just return a string for the application to then work on
    $name = isset($name) ? $name : 'world';
    return 'Hello, ' . $name;
});

// ... more routes ...

// handle the request, matching routes if possible
$response = $this->router->handleRequest();

If the "name" is given then we can use it, but if it's omitted the route will still match and we can fall back on the string "world"., (*9)

Variable validation

You can validate the path variables in your route by providing a list of validation callbacks, (*10)

$this->router->add('GET /hello/:name', [
    'name' => [
        function ($value) {
            // only accept short names
            return strlen($value) < 5;
        },
    ],
], function ($name) {
    return 'Hello, ' . $name;
});

Validation errors will raise an Exception detailing the variable that failed validation and the invalid value., (*11)

The Versions

08/04 2016

dev-0.2-dev

dev-0.2-dev

  Sources   Download

The Development Requires

by Mike Timms

24/03 2016

dev-master

9999999-dev

  Sources   Download

The Development Requires

by Mike Timms

24/03 2016

0.1

0.1.0.0

  Sources   Download

The Development Requires

by Mike Timms