R
R
r1ch2013-11-23 22:28:37
PHP
r1ch, 2013-11-23 22:28:37

Router implementation in php?

class CRouter
{
    public $controllerName = 'main';
    public $actionName = 'index';
    public $arrayGET = array(); 
    
    private $uri;
    
    private function getURI() {
        $request_uri = filter_input(\INPUT_SERVER, 'REQUEST_URI');
        $this->uri = isset($request_uri) ? $request_uri : '';
    }
    
    private function parseURI() {
        $routes = explode('/', $this->uri);
        if(!empty($routes[1])) {
            $this->controllerName = $routes[1];
        } else {
            return;
        }
        if(!empty($routes[2])) {
            $this->actionName = $routes[2];
        } else {
            return;
        }
        
        for($i = 3; $i < count($routes); $i+=2) {
            if(!empty($routes[$i]) && !empty($routes[$i + 1])) {
                $this->arrayGET[$routes[$i]] = $routes[$i + 1];    
            } else {
                return;
            }
        }
    }
        
    /**
    * This is the run
    */
    public function run() {
        $this->getURI();
        $this->parseURI();
    }

}
  

$route = new CRouter();
$route->run();

echo $route->controllerName.'/';
echo $route->actionName.'/';

foreach($route->arrayGET as $key => $value) {
    echo $key.'/'.$value.'/';
}

Correctness of the given approach of implementation of a router?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2013-11-24
@r1ch

http://noodlehaus.github.io/dispatch/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question