PHP router for HTTP requests.
Branch for PHP 8 support.
Route map as JSON file:
[
{
"controller": "Controller_Test",
"action": "test",
"pattern": "test :a (:b)",
"method": "CLI"
},
{
"controller": "Controller_Test",
"action": "test",
"pattern": "\/test\/:a\/(:b)",
"method": "GET"
}
]
As you can see, the router works for CLI and HTTP environments.
Controller class:
class Controller_Test
{
public function test( string $a = NULL, ?string $b = NULL ): string
{
return 'Called: Controller_Test::test($a, $b)' );
}
}
Router setup:
use CeusMedia\Router;
$source = Router\Registry\Source\JsonFile::create()->setResource( 'test.json' );
$registry = Router\Registry::create()->addSource( $source );
$router = Router\Router::create()->setRegistry( $registry );
Resolving a route:
$path = 'test a1 b2';
$route = $router->resolve( $path );
You could execute the resolved route controller action like this:
$result = \CeusMedia\Common\Alg\Obj\MethodFactory::staticCallClassMethod(
$route->getController(),
$route->getAction(),
[],
$route->getArguments()
);
$source = Router\Registry\Source\JsonFolder::create()->setResource( 'routes/' );
Invalidate cache if routes file has changed:
$invalidateCache = FALSE;
if( $invalidateCache ){
$memcache = new Memcache();
$memcache->connect( 'localhost', 11211 );
$memcache->delete( 'CeusMediaRouterCliDemo1' );
}
Setup, related to cache invalidation request:
$sourceMemcache = new Router\Registry\Source\Memcache( 'localhost:11211:CeusMediaRouterCliDemo1' );
$sourceMemcache->setOption( Router\Registry\SourceInterface::OPTION_AUTOSAVE, TRUE );
$sourceMemcache->setOption( Router\Registry\SourceInterface::OPTION_AUTOLOAD, !$invalidateCache );
...
$registry->addSource( $sourceMemcache );
$registry->addSource( $sourceJsonFile );