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()
$firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing();
qa_routing_config();
qa_controller_routing();
$route = qa_service('router')->match($requestlower);
if ($route !== null) {
// use new Controller system
qa_set_template($route->getId());
qa_set_template($route->getOption('template'));
$controllerClass = $route->getController();
$ctrl = new $controllerClass();
try {
......@@ -456,28 +456,28 @@ function qa_page_routing()
/**
* Set up routing.
*/
function qa_routing_config()
function qa_controller_routing()
{
$router = qa_service('router');
$router->addRoute('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
$router->addRoute('user', 'post', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
$router->addRoute('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index');
$router->addRoute('user-wall', 'get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall');
$router->addRoute('user-activity', 'get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity');
$router->addRoute('user-questions', 'get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions');
$router->addRoute('user-answers', 'get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers');
$router->addRoute('user-top', 'get', 'users', '\Q2A\Controllers\User\UsersList', 'top');
$router->addRoute('user-blocked', 'get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked');
$router->addRoute('user-new', 'get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest');
$router->addRoute('user-special', 'get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special');
$router->addRoute('ip', 'get', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address');
$router->addRoute('ip', 'post', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address');
$router->addRoute('admin-stats', 'get', 'admin/stats', '\Q2A\Controllers\Admin\Stats', 'index');
$router->addRoute('admin-points', 'get', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index');
$router->addRoute('admin-points', 'post', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index');
$router->addRoute('GET', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile', ['template' => 'user']);
$router->addRoute('POST', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile', ['template' => 'user']);
$router->addRoute('GET', 'user', '\Q2A\Controllers\User\UserProfile', 'index');
$router->addRoute('GET', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall', ['template' => 'user-wall']);
$router->addRoute('GET', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity', ['template' => 'user-activity']);
$router->addRoute('GET', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions', ['template' => 'user-questions']);
$router->addRoute('GET', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers', ['template' => 'user-answers']);
$router->addRoute('GET', 'users', '\Q2A\Controllers\User\UsersList', 'top', ['template' => 'users']);
$router->addRoute('GET', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked', ['template' => 'users']);
$router->addRoute('GET', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest', ['template' => 'users']);
$router->addRoute('GET', 'users/special', '\Q2A\Controllers\User\UsersList', 'special', ['template' => 'users']);
$router->addRoute('GET', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address', ['template' => 'ip']);
$router->addRoute('POST', 'ip/{str}', '\Q2A\Controllers\User\Ip', 'address', ['template' => 'ip']);
$router->addRoute('GET', 'admin/stats', '\Q2A\Controllers\Admin\Stats', 'index', ['template' => 'admin']);
$router->addRoute('GET', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index', ['template' => 'admin']);
$router->addRoute('POST', 'admin/points', '\Q2A\Controllers\Admin\Points', 'index', ['template' => 'admin']);
}
......
......@@ -21,9 +21,6 @@ namespace Q2A\Http;
class Route
{
/** @var string */
private $id;
/** @var string */
private $httpMethod;
/** @var string */
......@@ -36,25 +33,18 @@ class Route
private $action;
/** @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->routePath = $routePath;
$this->controller = $controller;
$this->action = $action;
$this->parameters = array();
}
/**
* @return string
*/
public function getId()
{
return $this->id;
$this->options = $options;
}
/**
......@@ -90,6 +80,14 @@ class Route
}
/**
* @return string
*/
public function getOption($name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}
/**
* @return array
*/
public function getParameters()
......
......@@ -21,27 +21,33 @@ namespace Q2A\Http;
class Router
{
/** @var Route[] */
protected $routes = array();
protected $routes = [];
/** @var array */
private $paramsConverter;
private $paramsConverter = [
'{str}' => '([^/]+)',
'{int}' => '([0-9]+)',
];
/** @var string */
private $httpMethod;
private $httpMethod = '';
public function __construct()
{
$this->paramsConverter = array(
'{str}' => '([^/]+)',
'{int}' => '([0-9]+)',
);
$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