Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active May 2, 2017 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harikt/243fb272a64761a089309b3338d9bee0 to your computer and use it in GitHub Desktop.
Save harikt/243fb272a64761a089309b3338d9bee0 to your computer and use it in GitHub Desktop.
Aura.Router version 3 example
{
"require": {
"aura/router": "^3.0",
"zendframework/zend-diactoros": "^1.3"
}
}
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Aura\Router\RouterContainer;
$routerContainer = new RouterContainer();
$map = $routerContainer->getMap();
$map->get('blog.read', '/blog/{id}', function ($request) {
$id = (int) $request->getAttribute('id');
$response = new Zend\Diactoros\Response();
$response->getBody()->write("You asked for blog entry {$id}.");
return $response;
});
$matcher = $routerContainer->getMatcher();
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
$route = $matcher->match($request);
if (! $route) {
echo " No route found";
exit;
}
foreach ($route->attributes as $key => $val) {
$request = $request->withAttribute($key, $val);
}
$callable = $route->handler;
// You should consider using https://github.com/auraphp/Aura.Dispatcher than the one line code below.
$response = $callable($request);
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header(sprintf('%s: %s', $name, $value), false);
}
}
echo $response->getBody();
@alinecrsouza
Copy link

Would be nice to have a working example of a call to an action in a Controller (with or without Aura Dispatcher).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment