1. Go to this page and download the library: Download bitexpert/pathfinder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bitexpert / pathfinder example snippets
$router = new \bitExpert\Pathfinder\Psr7Router();
$router->setRoutes(
[
new Route(['GET'], '/', 'index'),
new Route(['GET'], '/question/[:title]_[:id]', 'question'),
new Route(['GET', 'POST'], '/editquestion', 'editquestion')
]
);
use bitExpert\Pathfinder\RouteBuilder;
use bitExpert\Pathfinder\Matcher\NumericMatcher;
RouteBuilder::route()
->get('/')
->to('home')
->build();
RouteBuilder::route()
->from('/user')
->accepting('POST')
->accepting('PUT')
->to('userAction')
->build();
RouteBuilder::route()
->get('/')
->to(function () {
})
->named('home')
->build();
RouteBuilder::route()
->get('/user/[:userId]')
->to('userAction')
->ifMatches('userId', new NumericMatcher())
->build();
RouteBuilder::route()
->get('/user/[:userId]')
->to('userAction')
->ifMatches('userId', new NumericMatcher())
->build();
RouteBuilder::route()
->get('/user/[:userId]')
->to('userAction')
->ifMatches('userId', new NumericMatcher())
->ifMatches('userId', new MyCustomMatcher())
->ifMatches('userId', function ($value) {
})
->build();
use bitExpert\Pathfinder\Route;
use bitExpert\Pathfinder\Psr7Router;
use bitExpert\Pathfinder\Middleware\BasicRoutingMiddleware;
$router = new Psr7Router();
$router->addRoute(RouteBuilder::route()->get('/')->to('home')->build());
// The routing middleware will use the given router to match the request and will set the routing result as value
// of the request attribute named 'routingResult' for further use
$routingMiddleware = new BasicRoutingMiddleware($router, 'routingResult');
use bitExpert\Pathfinder\Psr7Router;
use bitExpert\Pathfinder\RouteBuilder;
use Zend\Diactoros\ServerRequest;
use bitExpert\Pathfinder\Matcher\NumericMatcher;
$router = new Psr7Router();
$homeRoute = RouteBuilder::route()
->get('/')
->to('home')
->build();
$orderUpdateRoute = RouteBuilder::route()
->put('/order/[:orderId]')
->to('updateOrder')
->ifMatches('id', new NumericMatcher())
->build();
$router->setRoutes([$homeRoute, $orderUpdateRoute]);
// Not found example
$request = new ServerRequest([], [], '/users', 'GET');
$result = $router->match($request);
$result->failed(); // -> true
$result->getFailure(); // -> RoutingResult::FAILED_NOT_FOUND
$result->hasRoute(); // -> false
// Method not allowed example
$request = new ServerRequest([], [], '/order/1', 'GET');
$result = $router->match($request);
$result->failed(); // -> true
$result->getFailure(); // -> RoutingResult::FAILED_METHOD_NOT_ALLOWED
$result->hasRoute(); // -> true
$result->getRoute(); // -> $orderUpdateRoute
// BadRequest example
$request = new ServerRequest([], [], '/order/abc', 'PUT');
$result = $router->match($request);
$result->failed(); // -> true
$result->getFailure(); // -> RoutingResult::FAILED_BAD_REQUEST
$result->hasRoute(); // -> true
$result->getRoute(); // -> $orderUpdateRoute
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.