I've been working on tweaking the dispatcher to make use of the autoloader and namespaces to allow drop-n-play addons to hook actions from their isolated directories under addons/.
In doing some research on how PHP frameworks handle dispatching, I came across numerous examples of routers and dispatchers . My knowledge of routers comes from the Golang scene, where I use routers as part of the Revel framework for my clients' websites . It seems that many have created router and dispatcher classes for PHP, which I think would be suitable to use in ElkArte.
At the moment, we have a primitive dispatcher which calls a class and method (and includes the files before my changes), based on two GET parameters: action and subaction. However, I think it would be nicer to see a richer router which could pass parameters to action controllers from anywhere in the URL.
This is, of course, not something that should be considered until 2.0. I personally really like the way that Revel handles routing, and I would suggest following suit, but I'm wondering what others' opinions are on this?
Here's an example of what I mean:
$router = new Router();
$router->add('GET', '/', '\\ElkArte\\Controllers\\BoardIndex_Controller', 'action_boardindex');
$router->add('GET', '/activate', '\\ElkArte\\Controllers\\Register_Controller', 'action_activate');
$router->add('GET', '/movetopic/:id', '\\ElkArte\\Controllers\\MoveTopic_Controller', 'action_movetopic');
$router->add('POST', '/movetopic', '\\ElkArte\\Controllers\\MoveTopic_Controller', 'action_movetopic2');
// Fallback
$router->add('GET', '/:action', '\\ElkArte\\Controllers\\:action_Controller', 'action_index');
$router->add('GET', '/:action/:sa', '\\ElkArte\\Controllers\\:action_Controller', 'action_:sa');
This would get rid of all the fallback checks we have currently in the dispatcher and smarten everything up. Any path section leading with : is a variable which is passed to the controller, or even used in the discovery of the class and method to call. Then the dispatcher just checks if it's a 'callable' and if not, defaults to /, which in this case is the board index.
By using the correct HTTP methods to detect the type of action, we could do away with all these <action>2 url forms by looking for POST requests.
Addons could then just use the following to add routes with parameters:
$router->add('GET', '/page/:id', '\\ElkArte\\Addons\\Pages\\Pages_Controller', 'action_page');
With a router setup like this, we can implement a router method for reversal, to generate URLs from actions. This would be useful for templates/themes, where links are required, making the URL scheme more flexible, and more human-friendly if required .
My examples follow the Revel router style, but any format would do. The standardisation and flexibility of a router and dispatcher is the important part. In Elkarte, it might be best to use PHP's built-in regex format.