2017 © Pedro Peláez
 

library router

A simple http request router library

image

prob/router

A simple http request router library

  • Sunday, October 23, 2016
  • by jongpak
  • Repository
  • 1 Watchers
  • 1 Stars
  • 285 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Prob/Router

A simple http request router library, (*1)

Build Status codecov, (*2)

Usage

1. Configure url mapping and handler

url.mapping.php, (*3)

<?php

use Prob\Router\Map;

$map = new Map();
$map->setNamespace('app\\controller');

$map->get('/', function ($url) {
    echo 'Hello main!';
});

$map->get('/test', 'Test.hello');
$map->get('/post/{post:int}', 'Post.view');

return $map;

2. Write your request handler (controller)

app/controller/Test.php, (*4)

<?php

namespace app\controller;

class Test
{
    public function hello()
    {
        echo 'Test page!';
    }
}

app/controller/Post.php, (*5)

<?php

namespace app\controller;

class Post
{
    public function hello($req)
    {
        echo 'Post ID: ' . $req['post'];
    }
}

3. Apply!

index.php, (*6)

<?php

use Prob\Router\Dispatcher;
use Prob\Rewrite\Request;

// use zend-diactoros package (for PSR-7)
use Zend\Diactoros\Request;
use Zend\Diactoros\Uri;

$dispatcher = new Dispatcher(require 'url.mapping.php');
// print 'Hello main'
$dispatcher->dispatch(
    (new Request())
        ->withUri(new Uri('http://test.com/'))
        ->withMethod('GET')
);
// print 'Test page!'
$dispatcher->dispatch(
    (new Request())
        ->withUri(new Uri('http://test.com/test'))
        ->withMethod('GET')
);
// print 'Post ID: 5'
$dispatcher->dispatch(
    (new Request())
        ->withUri(new Uri('http://test.com/post/5'))
        ->withMethod('GET')
);

The Versions

23/10 2016

dev-master

9999999-dev https://github.com/jongpak/prob-router

A simple http request router library

  Sources   Download

MIT

The Requires

 

The Development Requires

url dispatcher router path dispatch