2017 © Pedro Peláez
 

library router

PHP router as simple as can be

image

zkrati/router

PHP router as simple as can be

  • Tuesday, July 10, 2018
  • by zkrati
  • Repository
  • 0 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 15 Versions
  • 200 % Grown

The README.md

zkrati/router

PHP router as simple as can be..., (*1)

  • simple and fast yet complex routing
  • both static and dynamic routes
  • supports GET, POST, PUT, DELETE and PATCH requests

Version

1.0, (*2)

Basic usage

$router = new Zkrati\Routing\SimpleRouter();

$router->get("/test", function() {
    // handle GET request at /test
});

$router->post("/test", function() {
    // handle POST request at /test
});

$router->run();

simple right?, (*3)

You can also use other handler declarations, (*4)

$router->get("/test/path", "Class:methodName");
// this code will create an instance of Class and call it's method methodName

$router->get("/test/path", "Namespace\Class:methodName");
// if you are using namespaces

If you need to create your own instance for some reason, you can pass the created instance, (*5)

$instance = new Class();
$router->get("/testuju/path", array($instance, "methodName"));
// this code will use the given instance of Class and call it's method methodName

Variables

You will propably need to map routes with some varibles in it. It was never been easier., (*6)

$router->get("/test/<variable>/<next_variable>/path", function($variables) {
    // variables <variable> and <next_variable> are available in array $variables by it's keys
    // for example with url /test/example/showcase/path

    echo $variables["variable"];      // will output "example"
    echo $variables["next_variable"]; // will output "showcase"
});

Parameters

If you need to add the parameters to url, you can. It will be passed to your handler as second parameter., (*7)

$router->get("/test/<variable>/<next_variable>/path", function($variables, $params) {
    // for url /test/example/showcase/path?optional=true&param=john will passed array $params look like:
    // array(2) {
    //     ["optional"] => string(4) "true"
    //     ["param"] => string(4) "john"
    //   }
});

Url wildcards

If you are in situation when you are interested only in first half of the path you can use a wildcard., (*8)

$router->get("/test/path/*", "Class:methodName");
// this code will match all paths starting /test/path/ for example:
//  - /test/path/first
//  - /test/path/second
//  - /test/path/every/other/path

You can even use it as a wildcard for all requests, for example:, (*9)

$router->option("*", "Class:methodName");
// This will handle all option requests

You can also combine it with variables:, (*10)

$router->get("/test/<variable>/*", "Class:methodName");

Headers

Sometimes is useful to know the request headers. You don't need to search it somewhere anymore. Just add third parameter to your handler function., (*11)

$router->get("/test/<variable>/<next_variable>/path", function($variables, $params, $headers) {
    // variable $headers is array which contains all request headers 
});

Exceptions

It is a good idea to wrap route declarations into try catch because in case of invalid handler it will throw InvalidHandlerException, (*12)

try{

    $router->get("/test/", "Class:firstMethod");
    $router->get("/other/route", "Class:secondMethod");
    $router->get("/cool/route", "invalid handler");

} catch(InvalidHandlerException $e) {
    echo $e->getMessage();
}

also $router->run(); throws RouteNotFoundException when it founds no route for current url, (*13)

try{
    $router->run();
} catch(RouteNotFoundException $e) {
    echo $e->getMessage();
}

Instances

If you have custom class in your app which manages instances of all other classes and you want tou use string handler definition you can pass your instantiator into router. The router will get class instances from your custom instantiator., (*14)

$router->setInstantiator($instantiator, "getInstance");
// where $instantiator is your custom instantiator and "getInstance" is name of it´s method to get instance

Todos

  • define variable types and regexp
  • add cache support

License

MIT, (*15)

The Versions

10/07 2018

dev-master

9999999-dev

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

10/07 2018

1.0

1.0.0.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

09/07 2018

dev-issue-1

dev-issue-1

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

02/07 2018

0.6.1

0.6.1.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

01/07 2018

0.6

0.6.0.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

19/11 2017

0.5.1

0.5.1.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

18/11 2017

0.5

0.5.0.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

18/11 2017

0.4

0.4.0.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

11/08 2016

0.3

0.3.0.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

10/08 2016

0.2.1

0.2.1.0

PHP router as simple as can be

  Sources   Download

MIT

by Boomerank

09/08 2016

0.2

0.2.0.0

PHP router as simple as can be

  Sources   Download

GNU GPL

by Boomerank

09/08 2016

0.1.3

0.1.3.0

PHP router as simple as can be

  Sources   Download

GNU GPL

by Boomerank

09/08 2016

0.1.2

0.1.2.0

PHP router as simple as can be

  Sources   Download

GNU GPL

by Boomerank

08/08 2016

0.1.1

0.1.1.0

PHP router as simple as can be

  Sources   Download

GNU GPL

by Boomerank

08/08 2016

0.1

0.1.0.0

PHP router as simple as can be

  Sources   Download

GNU GPL

by Boomerank