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
The class covers the "route" to a given Controller ($className). Properties:
- uri (string, required)
- className (string, required)
- methods (array, optional)
- arguments (array, optional)
If the Route includes methods (which is optional) hasMethod validate the give one.
notice:
$method "HEAD" will be replaced with "GET"
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)
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
composer require terrazza/routing
- >= 7.4
- psr/log
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