HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Routing the CMS way

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
routingthecmsway

Problem

I always been curious on how to build great application with proper routing to my controllers, but after seeing some sources of applications like Xenforo, Joomla, it seems completely different, and mostly I can't understand them.

From my understanding the user sends a request to the web-server, the controller's job is to get the request, check out how is the request sending (to check if the request was done via XHR, etc) and then checking the sent GET requests and get the correct controller for the sent request, which will do the rest of the work.

This is what I exactly do currently, but I feel like my way is a bit not 'dynamic' as I have to put each controller's object into the switch statement.

This is my router class:

```
namespace library;

/**
* Controllers
*/

use library\controllers\Controller,
library\controllers\ChatController;

/**
* Class Router
* @package library
*/

class Router {

private $instance;

public function __construct(AsynChat $instance) {
$this->instance = $instance;
}

/**
* Deciding which routing channel to take.
*/

public function processRoutingChannel() {
if ($this->instance->getRequest()->isXHR()) {

$this->routeXHR();
return;
}
$this->route();
}

/**
* Handling a regular, visual web request.
*/

private function route() {
$request = $this->instance->getRequest();
$controller = null;

switch ($request->getGet(Config::$DEFAULT_GET_DIRECTOR)) {
default:
$controller = new ChatController($this->instance);
break;
}

$controller->register();
$controller->initializeTemplate();
}

/**
* Handling response for an AJAX request header.
* returns the needed data.
*/

private function routeXHR() {
$request = $this->instance->getRequest();
$controller = null;
switch ($request->getPost(Confi

Solution

Kid Diamond has asked for the opinions of other on the subject of routing and whats the best practice to code and he has shown his code.
Here is the link

Now I believe that what was said in that thread would be applicable to your thread too considering you wish to know about best way. So i would suggest to take a look at that.

Now secondly: Joomla, Magento, Wordpress and all that - should not be your base to design any routing system. Those aren't frameworks.

Take a look at Symfony 2, Laravel, etc for a better understanding because their routing system are packages and can work as a stand alone system.

As far as the code itself - I will leave those improvements to others to comment on.

Context

StackExchange Code Review Q#48597, answer score: 2

Revisions (0)

No revisions yet.