Commit e572049e by Scott

Refactor routing format

Replace id with an options parameter, add template option
parent 41f86fe8
...@@ -180,12 +180,12 @@ function qa_get_request_content() ...@@ -180,12 +180,12 @@ function qa_get_request_content()
$firstlower = strtolower($requestparts[0]); $firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing(); $routing = qa_page_routing();
qa_routing_config(); qa_controller_routing();
$route = qa_service('router')->match($requestlower); $route = qa_service('router')->match($requestlower);
if ($route !== null) { if ($route !== null) {
// use new Controller system // use new Controller system
qa_set_template($route->getId()); qa_set_template($route->getOption('template'));
$controllerClass = $route->getController(); $controllerClass = $route->getController();
$ctrl = new $controllerClass(); $ctrl = new $controllerClass();
try { try {
...@@ -456,28 +456,28 @@ function qa_page_routing() ...@@ -456,28 +456,28 @@ function qa_page_routing()
/** /**
* Set up routing. * Set up routing.
*/ */
function qa_routing_config() function qa_controller_routing()
{ {
$router = qa_service('router'); $router = qa_service('router');
$router->addRoute('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'); $router->addRoute('GET', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile', ['template' => 'user']);
$router->addRoute('user', 'post', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'); $router->addRoute('POST', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile', ['template' => 'user']);
$router->addRoute('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'); $router->addRoute('GET', 'user', '\Q2A\Controllers\User\UserProfile', 'index');
$router->addRoute('user-wall', 'get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'); $router->addRoute('GET', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall', ['template' => 'user-wall']);
$router->addRoute('user-activity', 'get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'); $router->addRoute('GET', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity', ['template' => 'user-activity']);
$router->addRoute('user-questions', 'get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'); $router->addRoute('GET', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions', ['template' => 'user-questions']);
$router->addRoute('user-answers', 'get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'); $router->addRoute('GET', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers', ['template' => 'user-answers']);
$router->addRoute('user-top', 'get', 'users', '\Q2A\Controllers\User\UsersList', 'top'); $router->addRoute('GET', 'users', '\Q2A\Controllers\User\UsersList', 'top', ['template' => 'users']);
$router->addRoute('user-blocked', 'get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'); $router->addRoute('GET', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked', ['template' => 'users']);
$router->addRoute('user-new', 'get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'); $router->addRoute('GET', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest', ['template' => 'users']);
$router->addRoute('user-special', 'get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'); $router->addRoute('GET', 'users/special', '\Q2A\Controllers\User\UsersList', 'special', ['template' => 'users']);
$router->addRoute('ip', 'get', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address'); $router->addRoute('GET', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address', ['template' => 'ip']);
$router->addRoute('ip', 'post', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address'); $router->addRoute('POST', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address', ['template' => 'ip']);
$router->addRoute('admin-stats', 'get', 'admin/stats', '\Q2A\Controllers\Admin\Stats', 'index'); $router->addRoute('GET', 'admin/stats', '\Q2A\Controllers\Admin\Stats', 'index', ['template' => 'admin']);
$router->addRoute('admin-points', 'get', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index'); $router->addRoute('GET', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index', ['template' => 'admin']);
$router->addRoute('admin-points', 'post', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index'); $router->addRoute('POST', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index', ['template' => 'admin']);
} }
......
...@@ -21,9 +21,6 @@ namespace Q2A\Http; ...@@ -21,9 +21,6 @@ namespace Q2A\Http;
class Route class Route
{ {
/** @var string */ /** @var string */
private $id;
/** @var string */
private $httpMethod; private $httpMethod;
/** @var string */ /** @var string */
...@@ -36,25 +33,18 @@ class Route ...@@ -36,25 +33,18 @@ class Route
private $action; private $action;
/** @var array */ /** @var array */
private $parameters; private $options;
public function __construct($id = null, $httpMethod = null, $routePath = null, $controller = null, $action = null) /** @var array */
private $parameters = [];
public function __construct($httpMethod, $routePath, $controller = '', $action = '', array $options = [])
{ {
$this->id = $id;
$this->httpMethod = strtoupper($httpMethod); $this->httpMethod = strtoupper($httpMethod);
$this->routePath = $routePath; $this->routePath = $routePath;
$this->controller = $controller; $this->controller = $controller;
$this->action = $action; $this->action = $action;
$this->options = $options;
$this->parameters = array();
}
/**
* @return string
*/
public function getId()
{
return $this->id;
} }
/** /**
...@@ -90,6 +80,14 @@ class Route ...@@ -90,6 +80,14 @@ class Route
} }
/** /**
* @return string
*/
public function getOption($name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}
/**
* @return array * @return array
*/ */
public function getParameters() public function getParameters()
......
...@@ -21,27 +21,33 @@ namespace Q2A\Http; ...@@ -21,27 +21,33 @@ namespace Q2A\Http;
class Router class Router
{ {
/** @var Route[] */ /** @var Route[] */
protected $routes = array(); protected $routes = [];
/** @var array */ /** @var array */
private $paramsConverter; private $paramsConverter = [
'{str}' => '([^/]+)',
'{int}' => '([0-9]+)',
];
/** @var string */ /** @var string */
private $httpMethod; private $httpMethod = '';
public function __construct() public function __construct()
{ {
$this->paramsConverter = array(
'{str}' => '([^/]+)',
'{int}' => '([0-9]+)',
);
$this->httpMethod = strtoupper($_SERVER['REQUEST_METHOD']); $this->httpMethod = strtoupper($_SERVER['REQUEST_METHOD']);
} }
public function addRoute($id, $httpMethod, $routePath, $class, $func) /**
* Add a new URI handler to the router.
* @param string $httpMethod
* @param string $routePath
* @param string $class The controller to use.
* @param string $func The class method to call.
* @param array $options Extra parameters e.g. Q2A template the page should use.
*/
public function addRoute($httpMethod, $routePath, $class, $func, array $options = [])
{ {
$this->routes[] = new Route($id, $httpMethod, $routePath, $class, $func); $this->routes[] = new Route($httpMethod, $routePath, $class, $func, $options);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment