obscure-code / router
PHP router
1.5
2023-09-29 19:36 UTC
Requires
- php: >=8.0
Requires (Dev)
- mockery/mockery: ~1.6
- phpstan/phpstan: ~1.10
- phpunit/phpunit: ~10.3
- slevomat/coding-standard: ~8.13
- squizlabs/php_codesniffer: ~3.7
- vimeo/psalm: ~5.15
README
- PHP >= 8.0
- Composer
Installation
$ composer require obscure-code/router
Basic Usage
Create Router class:
use ObscureCode\Router as AbstractRouter; class Router extends AbstractRouter { public function patternDefault(string $path): void { $this->load('header'); $this->load($path); $this->load('footer'); } public function patternBlank(string $path): void { $this->load($path); } public function patternError(string $path): void { http_response_code(404); $this->load('header'); $this->load('error'); $this->load('footer'); } };
Create config:
$config = [ "index" => [ "pattern" => "default", ], "ajax" => [ "pattern" => "blank", ], "directory" => [ "index" => [ "pattern" => "default", ], ], "error" => [ "pattern" => "error", ], ];
Create Router instance with root directory and config:
$router = new Router( __DIR__ . '/include', $config, );
Examples:
// Call /include/index.php by default $router->call('/'); // Call /include/directory/index.php $router->call('/directory/index'); // If /include/a/b.php route exists, ['c', 'd', 'e'] will passed in $params // If only /include/a.php exists, ['b', 'c', 'd', 'e'] will passed in $params $router->call('/a/b/c/d/e'); // ['data'] will passed in $data $router->call('/index', ['data']);
Also you can pass data in load
method:
public function patternDefault(string $path): void { $someClass = new SomeClass(); $this->load('header'); $this->load($path, ['someClass' => $someClass]); $this->load('footer'); }
If route not found in config, NotFoundException
will be thrown.
So you can use router like that:
use ObscureCode\Exceptions\NotFoundException; try { ob_start(); $router->call($route); } catch (NotFoundException $exception) { ob_clean(); $router->call('error'); } catch (SomeOtherException $exception) { // do something } finally { ob_end_flush(); }
If route exist in config, but pattern or file not exists, LogicException
will be thrown.
You can see an example of a boilerplate application here.
Local development
docker build --tag router .
docker run --detach -v "$(pwd):/app" --name=router router
docker exec -it router /bin/bash
composer install
composer tests
composer cs
composer stan
composer psalm