Example #1
0
 public static function initialise()
 {
     require_once 'vendor/autoload.php';
     try {
         require_once 'lib/routing.php';
         $uri = Routing::fetch_uri();
         $controller = ucfirst($uri['controller']) . 'Controller';
         @(include "controllers/{$uri['controller']}_controller.php");
         if (substr($uri['action'], 0, 1) == '?') {
             $uri['action'] = '';
         }
         if (empty($uri['action']) && method_exists($controller, 'index')) {
             $uri['action'] = 'index';
         }
         // If controller found and action exists
         if (class_exists($controller) && method_exists($controller, $uri['action'])) {
             $app = new $controller();
         } else {
             $uri = Routing::route();
             $controller = ucfirst($uri['controller']) . 'Controller';
             include "controllers/{$uri['controller']}_controller.php";
             $app = new $controller();
         }
         $app->loadConfig();
         $app->loadTwig();
         $app->loadAws();
         $app->loadModels();
         $app->loadPlugins();
         $app->uri = $uri;
         // Helper var to simplify reponding to json
         $app->json = $app->uri['format'] == 'json';
         require_once 'lib/filter.php';
         $app->runFilters();
         $app->loadDefaultLibs();
         // Set timezone from config
         date_default_timezone_set($config->timezone);
         // Call relevant function in controller
         $app->loadAction();
         unset($_SESSION['flash']);
     } catch (ValidationException $e) {
         ob_end_clean();
         Application::flash('error', $e->getMessage());
         header('Location: ' . $_SERVER['HTTP_REFERER']);
     } catch (RoutingException $e) {
         ob_end_flush();
         // RoutingExceptions only thrown from static context
         // so must set up new Application before rendering 404
         $app = new Application();
         $app->loadView('pages/404');
     } catch (ApplicationException $e) {
         ob_end_flush();
         $e->app->loadView('pages/500');
     }
 }