2017 © Pedro Peláez
 

library routing

Simple router to GET and POST requests.

image

tdw/routing

Simple router to GET and POST requests.

  • Monday, December 25, 2017
  • by tiago.desenvolvedorweb
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Routing

Simple router to GET, POST, PUT, PATCH, DELETE requests., (*1)

Build Status, (*2)

Requirements

PHP: >=7.0, (*3)

Install

$ composer require tdw/routing

require 'to/path/vendor/autoload.php';

Usage

Instance, (*4)

<?php

$router = new \Tdw\Routing\Router();

Closure, (*5)

<?php

$router->addGET(new \Tdw\Routing\Route('/', function (){
    echo 'Home page';
}, 'home'));

Action and route name, (*6)

<?php

class ArticleAction
{
    /**
     * Route GET
     */
    public function index()
    {
        //
    }

    /**
     * Route POST
     */
    public function save()
    {
        //
    }
}

$router->addGET(new \Tdw\Routing\Route('/articles', 'ArticleAction@index', 'article.index'));
$router->addPOST(new \Tdw\Routing\Route('/articles/save', 'ArticleAction@save', 'article.save'));

Rule, (*7)

<?php

$route = (new \Tdw\Routing\Route('/article/{slug}/{id}', function () {
    //
}, 'articles.show'))
    ->addRule('slug', new \Tdw\Routing\Rule\Slug())
    ->addRule('id', new \Tdw\Routing\Rule\Id());
$router->addGET($route);

Match, (*8)

<?php

$currentRoute = $router->match(\GuzzleHttp\Psr7\ServerRequest::fromGlobals());

var_dump($currentRoute);

Exceptions

\Tdw\Routing\Exception\RouteNotFoundException
\Tdw\Routing\Exception\RouteNameNotFoundException

Suport for methods

- GET
- POST
- PUT
- PATCH
- DELETE

Integration suggestion

<?php

class App
{
    /**
     * @var \Tdw\Routing\Contract\Router
     */
    private $router;

    function __construct(\Tdw\Routing\Contract\Router $router)
    {
        $this->router = $router;
    }

    function run(\Psr\Http\Message\ServerRequestInterface $request)
    {
        if ($route = $this->router->match($request)) {
            if ($route->getCallback() instanceof Closure) {
                return call_user_func_array($route->getCallback(), $route->getParameters());
            }
            list( $action, $method ) = explode('@', $route->getCallback());
            return call_user_func_array([new $action, $method], $route->getParameters());

        }
        return new \GuzzleHttp\Psr7\Response(404, [], 'Page Not Found');
    }
}

(new App($router))->run(\GuzzleHttp\Psr7\ServerRequest::fromGlobals());

Contributing

Add new features., (*9)

License

The Tdw Routing is open-sourced software licensed under the MIT license., (*10)

The Versions

25/12 2017

dev-master

9999999-dev

Simple router to GET and POST requests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tiago Lopes

25/12 2017

1.0.0

1.0.0.0

Simple router to GET and POST requests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tiago Lopes

18/11 2017

0.9.9

0.9.9.0

Simple router to GET and POST requests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tiago Lopes

02/10 2017

0.9.8

0.9.8.0

Simple router to GET and POST requests.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tiago Lopes