Skip to content

draftmode-org/routing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

the routing component

The component match routes.
The component does not include any class loading or injection.
The only goal is: find/match routes

  1. Object/Classes
    1. Route
    2. RouteSearch
    3. RouteMatcher
  2. Install
  3. Requirements
  4. 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.

  1. match Route->uri against routeSearch->uri + method + arguments
  2. 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      

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages