Created
May 27, 2020 14:22
-
-
Save kobus1998/99e2f5b9743916d99c6146be88deda64 to your computer and use it in GitHub Desktop.
aura router integrated with psr container
This file contains 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 __DIR__ . '/vendor/autoload.php'; | |
use League\Container\Container; | |
$container = new Container(); | |
$container->add('helper', Helper::class); | |
$router = new Routing($container); | |
$map = $router->getMap(); | |
$map->get('home', '/home', ['Controller', 'home']); | |
$map->get('home2', '/', ['Controller', 'home']); | |
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(); | |
$router->handle($request); |
This file contains 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 | |
class Routing extends \Aura\Router\RouterContainer | |
{ | |
protected $container; | |
public function __construct($container) | |
{ | |
parent::__construct(); | |
$this->container = $container; | |
} | |
public function handle($request) | |
{ | |
$matcher = $this->getMatcher(); | |
$route = $matcher->match($request); | |
$callback = $route->handler; | |
if (is_array($callback)) { | |
$controllerName = reset($callback); | |
$methodName = end($callback); | |
$controller = new $controllerName($this->container); | |
return $controller->{$methodName}($request); | |
} | |
return $callback($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment