Beispiel #1
0
 function __construct()
 {
     $url = isset($_GET["url"]) ? $_GET["url"] : NULL;
     $url = rtrim($url, '/');
     $url = explode('/', $url);
     if (empty($url[0]) || $url[0] == 'index.php') {
         require 'controller/IndexController.php';
         $controller = new IndexController();
         $controller->index();
         return FALSE;
     }
     $file = 'controller/' . $url[0] . 'Controller.php';
     if (file_exists($file)) {
         require $file;
     } else {
         require 'controller/ErrorController.php';
         $error = new ErrorController();
         $error->index();
         return FALSE;
     }
     $controllerFullName = $url[0] . "Controller";
     $controller = new $controllerFullName();
     if (isset($url[2])) {
         if (method_exists($controller, $url[1])) {
             $controller->{$url[1]}($url[2]);
         } else {
             require 'controller/ErrorController.php';
             $error = new ErrorController();
             $error->index();
             return FALSE;
         }
     } else {
         if (isset($url[1])) {
             if (method_exists($controller, $url[1])) {
                 $controller->{$url[1]}();
             } else {
                 require 'controller/ErrorController.php';
                 $error = new ErrorController();
                 $error->index();
                 return FALSE;
             }
         } else {
             $controller->index();
         }
     }
 }
Beispiel #2
0
 public function launch()
 {
     $controller = ucfirst($this->controller) . 'Controller';
     if (class_exists($controller)) {
         $controller = new $controller();
     } else {
         $controller = new ErrorController();
         return $controller->index('404: ' . $this->controller . ' not found.');
     }
     if (!$controller->restful) {
         $method = 'action_' . $this->method;
     } else {
         $method = strtolower($_SERVER['REQUEST_METHOD']) . "_" . $this->method;
     }
     if (method_exists($controller, $method)) {
         return call_user_func_array(array($controller, $method), $this->args);
     } else {
         return $controller->index("404: Method " . str_replace('get_', '', "'{$method}' not exists."));
     }
 }
Beispiel #3
0
 /**
  * "Start" the application:
  * Analyze the URL elements and calls the according controller/method or the fallback
  */
 public function __construct()
 {
     // create array with URL parts in $url
     $this->splitUrl();
     // check for controller: no controller given ? then load start-page
     if (!$this->url_controller) {
         Home::index();
     } elseif (file_exists(APP_PATH . 'controllers/' . $this->url_controller . '.php')) {
         // here we did check for controller: does such a controller exist ?
         // if so, then load this file and create this controller
         // example: if controller would be "car", then this line would translate into: $this->car = new car();
         $this->url_controller = new $this->url_controller();
         // check for method: does such a method exist in the controller ?
         if (method_exists($this->url_controller, $this->url_action)) {
             if (!empty($this->url_params)) {
                 // Call the method and pass arguments to it
                 call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params);
             } else {
                 // If no parameters are given, just call the method without parameters, like $this->home->method();
                 $this->url_controller->{$this->url_action}();
             }
         } else {
             if (strlen($this->url_action) == 0) {
                 // no action defined: call the default index() method of a selected controller
                 $this->url_controller->index();
             } else {
                 // defined action not existent: show the error page
                 $page = new ErrorController();
                 $page->index();
             }
         }
     } else {
         $page = new ErrorController();
         $page->index();
     }
 }
Beispiel #4
0
 /**
  * Configure everything, then call controller, then call view.
  * 
  * @return void
  */
 public function run()
 {
     session_start();
     /* BASIC CONSTANTS */
     // Root path on the server filesystem.
     $root_path = rtrim(pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME), '/');
     // Root URI for the site.
     $proto = 'http://';
     if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
         $proto = 'https://';
     }
     $scr_dir = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
     $scr_uri = $proto . $_SERVER['HTTP_HOST'] . $scr_dir;
     $root_uri = rtrim($scr_uri, '/');
     // Have those three available everywhere.
     define('ROOT_PATH', $root_path);
     define('DS', DIRECTORY_SEPARATOR);
     define('ROOT_URI', $root_uri);
     /* ROUTE IT */
     $this->routes = new Routes();
     // array url_elements is the main routing container. See bellow.
     $url_info = substr($_SERVER['REQUEST_URI'], strlen($scr_dir));
     // check for defined static routes
     if (array_key_exists($url_info, $this->routes->static_routes)) {
         $url_info = $this->routes->static_routes[$url_info];
     }
     $url_info = trim($url_info, '/');
     $url_elements = explode('/', $url_info);
     if ('index' == $url_elements[0] || 'index.php' == $url_elements[0]) {
         array_shift($url_elements);
     }
     // Prefixed routes. Always lowercase.
     $prefx = '';
     if (!empty($url_elements[0])) {
         if (in_array($url_elements[0], $this->routes->prefixes)) {
             $prefx = array_shift($url_elements);
         }
     }
     define('PREFIX', strtolower($prefx));
     // Static pages
     // No url parameters
     $ctlr_name = !empty($url_elements[0]) ? $url_elements[0] : '';
     // url parameter is among static pages
     if (PREFIX == '') {
         if (in_array($ctlr_name, $this->routes->static_pages)) {
             array_unshift($url_elements, 'pages');
             $ctlr_name = 'pages';
         }
     } else {
         // static page in prefixed routes
         if (array_key_exists(PREFIX, $this->routes->prefixes_with_stpages)) {
             if (in_array($ctlr_name, $this->routes->prefixes_with_stpages[PREFIX]) || $ctlr_name == '') {
                 array_unshift($url_elements, 'pages');
                 $ctlr_name = 'pages';
             }
         }
     }
     $compl_ctlr_name = ucfirst($ctlr_name . 'Controller');
     // default action is index
     $action = isset($url_elements[1]) ? $url_elements[1] : 'index';
     // ErrorController
     require_once ROOT_PATH . DS . 'controllers' . DS . 'ErrorController.php';
     $controller_path = DS . 'controllers' . DS;
     if (PREFIX != '') {
         $controller_path = DS . 'controllers' . DS . PREFIX . DS;
     }
     if (is_file(ROOT_PATH . $controller_path . $compl_ctlr_name . '.php')) {
         require_once ROOT_PATH . $controller_path . $compl_ctlr_name . '.php';
     } else {
         $controller_path = DS . 'controllers' . DS;
         $compl_ctlr_name = 'ErrorController';
         $ctlr_name = 'error';
         Out::flash('Controller not found');
     }
     // Accept json and xml extensions. Call _json, _xml.
     if (strtolower(substr($action, -5)) == '.json') {
         $action = str_replace('.json', '_json', $action);
     }
     if (strtolower(substr($action, -4)) == '.xml') {
         $action = str_replace('.xml', '_xml', $action);
     }
     /* CONTROLLER */
     // Start action
     $ctlr = $compl_ctlr_name;
     $this->controller = new $ctlr($this);
     if (method_exists($this->controller, $action)) {
         // Call method. Everything after method name, becomes a parameter.
         call_user_func_array(array($this->controller, $action), array_slice($url_elements, 2));
     } else {
         $error_controller = new ErrorController($this);
         $error_controller->index();
         Out::flash('Action not found');
     }
     /* VIEW */
     $this->view = new BaseView($this);
     // Set default pg_name to be rendered.
     if (empty($this->pg_name)) {
         $this->pg_name = strtolower($ctlr_name) . DS . strtolower($action);
         if (PREFIX != '') {
             $this->pg_name = PREFIX . DS . $this->pg_name;
         }
     }
     // If pg_name set to 'no_view' it gives output from the controller.
     // Use it to output json, xml, ...
     //
     // $this->app->pg_name = 'pages/override'; in the controller
     // overrides the default page
     if (!($this->pg_name == 'no_view')) {
         $this->view->render($this->pg_name);
     }
 }