swoole-framwork/core/init/RouterCollector.php

31 lines
674 B
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
namespace Core\init;
use Core\annotations\Bean;
/**
* @Bean()
*/
class RouterCollector
{
public $routes = [];
public function addRouter($method, $uri, $handler)
{
$this->routes[] = [
'method' => $method,
'uri' => $uri,
'handler' => $handler
];
}
public function getDispatcher()
{
//注册路由
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r){
foreach($this->routes as $route){
$r->addRoute($route['method'],$route['uri'],$route['handler']);
}
});
return $dispatcher;
}
}