-
-
Save graphis/e289bcd181427d84da3996216353083a to your computer and use it in GitHub Desktop.
Aura.Router version 3 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"aura/router": "^3.0", | |
"zendframework/zend-diactoros": "^1.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment