PHP Classes

File: router/Router.php

Recommend this page to a friend!
  Classes of Adrian M   Basic PHP MVC Basic Model   router/Router.php   Download  
File: router/Router.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Basic PHP MVC Basic Model
Implement MVC applications with a router class
Author: By
Last change:
Date: 10 months ago
Size: 703 bytes
 

Contents

Class file image Download
<?php
class Router {
    private
$routes = [];

    public function
addRoute($route, $controller) {
       
$this->routes[$route] = $controller;
    }

    public function
dispatch($url) {
        if (
array_key_exists($url, $this->routes)) {
           
$controller = $this->routes[$url];
           
$this->callController($controller);
        } else {
           
// Handle 404 Not Found
            //$this->callController('NotFoundController');
           
echo "NotFoundController";
        }
    }

    private function
callController($controller) {
        include_once(
"controllers/{$controller}.php");
       
$controllerObject = new $controller();
       
$controllerObject->invoke();
    }
}