Last active
May 2, 2017 19:46
-
-
Save harikt/243fb272a64761a089309b3338d9bee0 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
Would be nice to have a working example of a call to an action in a Controller (with or without Aura Dispatcher).