Exemple #1
0
 /**
  * Get controller based on HTTP request
  * @param array $args Controller arguments
  * @return object Controller
  * @throws Exception\UndefinedControllerException
  * @throws Exception\InvalidControllerException
  * @throws Exception\NoIndexRouteException
  */
 public static function getController(array $args = array())
 {
     $config = ConfigFileHandler::getInstance();
     $mvc = $config->getParam("framework", "mvc");
     $requestArray = Request::getDirArray();
     $routePath = $mvc["route"];
     $controllerClassName = null;
     if (!isset($requestArray[intval($mvc["start_index"])])) {
         $controllerClassName = $routePath["."];
     } else {
         for ($i = intval($mvc["start_index"]); $i < count($requestArray) + 1; $i++) {
             if (!($routePath = self::getRouteChild($requestArray[$i], $routePath))) {
                 throw new UndefinedControllerException("Controller for this request is not defined");
             }
             if (is_string($routePath)) {
                 $controllerClassName = $routePath;
                 break;
             }
             if (!isset($requestArray[$i + 1])) {
                 if (isset($routePath["."])) {
                     $controllerClassName = $routePath["."];
                     break;
                 } else {
                     throw new NoIndexRouteException("Route has no index controller defined");
                 }
             }
         }
     }
     $controllerReflection = new \ReflectionClass(__NAMESPACE__ . "\\Controller\\" . $controllerClassName);
     if ($controllerReflection->implementsInterface(__NAMESPACE__ . "\\Controller\\Init\\IController")) {
         return $controllerReflection->newInstance($args);
     } else {
         throw new InvalidControllerException("Controller is not implementing interface IController");
     }
 }
Exemple #2
0
require_once "src/FreshPHP/Config/ClassAutoloader.php";
use FreshPHP\Config\ClassAutoloader;
use FreshPHP\Config\ConfigFileHandler;
use FreshPHP\Config\LocaleTransfer;
use FreshPHP\HTTP\Request;
use FreshPHP\MVC\MVCRouter;
use FreshPHP\MVC\Controller\ErrorController;
session_start();
ClassAutoloader::register() || die("Cannot register autoloader");
error_reporting(Request::getVariable("debug") ? E_ALL : 0);
$lDir = (int) ConfigFileHandler::getInstance()->getParam("framework", "mvc", "locale_index");
if ($lDir >= 0 && Request::getDir($lDir) == "") {
    Request::redirect("/" . !Request::getSessionVar("locale", "%s", false) ? ConfigFileHandler::getInstance()->getParam("framework", "mvc", "default_locale") : Request::getSessionVar("locale"));
}
try {
    LocaleTransfer::setLocale(Request::getDir((int) ConfigFileHandler::getInstance()->getParam("framework", "mvc", "locale_index")));
    Request::setSessionVar("locale", LocaleTransfer::getLocale());
    MVCRouter::getController()->main();
} catch (Exception $e) {
    $errorController = new ErrorController();
    $errorData = array();
    $exception = explode("\\", get_class($e));
    switch ($exception[count($exception) - 1]) {
        case "NoIndexRouteException":
        case "UndefinedControllerException":
            $errorData["code"] = 404;
            break;
        case "InvalidControllerException":
            $errorData["code"] = 500;
            break;
        case "Exception":