terrazza / routing
Terrazza Component Routing
dev-main
2022-05-11 22:27 UTC
Requires
- php: >=7.4
- psr/log: 1.1.*
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-05-12 02:46:15 UTC
README
The component match routes.
The component does not include any class loading or injection.
The only goal is: find/match routes
- Object/Classes
- Install
- Requirements
- Examples
Object/Classes
Route
The class covers the "route" to a given Controller ($className). Properties:
- uri (string, required)
- className (string, required)
- methods (array, optional)
- arguments (array, optional)
method: hasMethod(string $method) : bool
If the Route includes methods (which is optional) hasMethod validate the give one.
notice:
$method "HEAD" will be replaced with "GET"
RouteSearch
The class covers the base uri, method and arguments. All Routes are matched against this object.
Properties:
- uri (string, required)
- method (string, required, default=GET)
- arguments (array, optional)
RouteMatcher
method: getRoute(RouteSearch $routeSearch, array $routes) :?Route
To get a route is done in two steps.
- match Route->uri against routeSearch->uri + method + arguments
- get all public methods within the annotation @Route/uri and match against routeSearch + method + arguments
The annotation URI is without the Controller Route Uri and is required
How to install
Install via composer
composer require terrazza/routing
Requirements
php version
- >= 7.4
composer packages
- psr/log
Examples
notice: The example requires a Psr\Log\LoggerInterface implementation
use Terrazza\Component\Routing\Route;
use Terrazza\Component\Routing\RouteMatcher;
use Terrazza\Component\Routing\RouteSearch;
class ControllerPayment {
/**
* @Route/method GET
* @Route/uri /view1
* @return string
*/
function methodView1() : string {
return "methodView1";
}
/**
* @Route/method GET
* @Route/uri /{id}
* @return string
*/
function methodById() : string {
return "methodById";
}
/**
* @Route/method GET
* @Route/uri /view3
* @return string
*/
function methodView3() : string {
return "methodView3";
}
}
class ControllerPaymentView {
/**
* @Route/method GET
* @Route/uri /{id}
* @return string
*/
function paymentView() : string {
return "paymentView";
}
}
$routes = [
new Route("payment", ControllerPayment::class),
new Route("payment/view", ControllerPaymentView::class),
];
//
// Psr\Log\LoggerInterface implementation
//
$logger = "IMPORTANT ! has to initialized";
echo (new RouteMatcher($logger))
->getRoute(new RouteSearch("payment/view1"), $routes)->getClassMethodName();
// found in ControllerPayment, method methodView1
echo (new RouteMatcher($logger))
->getRoute(new RouteSearch("payment/view/1"), $routes)->getClassMethodName();
// found ControllerPayment, method methodView2 but will be skipped cause $id includes /
// found in ControllerPaymentView, method paymentView
echo (new RouteMatcher($logger))
->getRoute(new RouteSearch("payment/view3"), $routes)->getClassMethodName(),
// found in ControllerPayment, method methodView3
echo (new RouteMatcher($logger))
->getRoute(new RouteSearch("payment/view4"), $routes)->getClassMethodName(),
// found in ControllerPayment, method methodById