Skip to content

Commit

Permalink
Merge pull request #44 from sukui/router_support_action_params
Browse files Browse the repository at this point in the history
update router interface
  • Loading branch information
zxcvdavid authored Aug 2, 2017
2 parents feeba2d + df6229b commit 462ece9
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Network/Http/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function dispatch(Request $request, Context $context)
{
$controllerName = $context->get('controller_name');
$action = $context->get('action_name');
$args = $context->get('action_args');

$controller = $this->getControllerClass($controllerName);
if(!class_exists($controller)) {
Expand All @@ -24,7 +25,7 @@ public function dispatch(Request $request, Context $context)
if(!is_callable([$controller, $action])) {
throw new PageNotFoundException("action:{$action} is not callable in controller:" . get_class($controller));
}
yield $controller->$action();
yield call_user_func_array([$controller,$action],$args);
}

private function getControllerClass($controllerName)
Expand Down
1 change: 1 addition & 0 deletions src/Network/Http/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private function initContext(Request $request, SwooleHttpRequest $swooleRequest,
return false;
$this->context->set('controller_name', $route['controller_name']);
$this->context->set('action_name', $route['action_name']);
$this->context->set('action_args', $route['action_args']);

$cookie = new Cookie($request, $swooleResponse);
$this->context->set('cookie', $cookie);
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Http/Routing/IRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
interface IRouter
{
/*
* @return array ['module', 'controller', 'action']
* @return array ['module', 'controller', 'action', 'params']
*/
public function dispatch(Request $request);
}
6 changes: 3 additions & 3 deletions src/Network/Http/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function init($config)
} else {
sys_error("$routerClass should be an implementation of IRouter");
}

}
$this->routerRule = new ZanRouter();
}
Expand Down Expand Up @@ -97,10 +96,11 @@ public function handleUri($uri)
private function parseRoute($request)
{
$result = $this->routerRule->dispatch($request);
if (!is_array($result) || count($result) != 3) {
if (!is_array($result) || count($result) != 4) {
throw new \Exception("Route dispatch failed");
}
list($module, $controller, $action) = $result;
list($module, $controller, $action, $args) = $result;
$route['action_args'] = $args;
$route['action_name'] = $action;
$route['controller_name'] = $module.$this->separator.$controller;
return $route;
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Http/Routing/ZanRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public function dispatch(Request $request)
$actionName = array_pop($parts);
$controllerName = array_pop($parts);
$moduleName = join($separator, $parts);
return [$moduleName, $controllerName, $actionName];
return [$moduleName, $controllerName, $actionName,[]];
}
}

0 comments on commit 462ece9

Please sign in to comment.