public function __construct() { $this->authController = \core\Loader::load('\\auth\\controller\\AuthController'); self::$userIsloggedIn = $this->authController->userIsLoggedIn(); //WHY STATIC?? $this->settings = \core\Loader::load('\\blogg\\model\\admin\\Settings'); }
public function dispatch($testing = false) { $controllerName = $this->getController(); $action = $this->getAction(); $params = $this->getParams(); if (!file_exists(SRC_DIR)) { throw new \Exception("Something wrong with the configuration of this project check global SRC_DIR in definer.php"); } //Tar fram alla mappar i src mappen $files = scandir(SRC_DIR, 1); $controllerfile = ""; $namespace = ""; //Loopa dessa filer foreach ($files as $file) { $controllerDir = SRC_DIR . $file . DS . 'controller'; if (file_exists($controllerDir)) { //Om man hittar controller mappen $filesInControllerDir = scandir($controllerDir, 1); //Hämta alla filnamn i den mappen foreach ($filesInControllerDir as $fileInControllerDir) { if (0 === strpos($fileInControllerDir, $controllerName)) { $controllerfile = SRC_DIR . $file . DS . 'controller' . DS . $fileInControllerDir; $namespace = $file; //Hitttat rätt fil break; } } } } $controller = '\\' . $namespace . '\\controller\\' . ucfirst($controllerName) . 'Controller'; //Alltid stor första bokstav på objekt if (file_exists($controllerfile)) { require_once $controllerfile; try { $app = \core\Loader::load($controller); $app->setParams($params); if (!method_exists($app, $action)) { throw new \Exception('Controller ' . $controller . ' does not have ' . $action . ' function'); } $app->{$action}(); //Denna delen bör förändras, men vet inte exakt hur jag ska göra och vilket objekt som ska ansvara för renderingen $view = $app->getView(); $view->render($action); } catch (\PDOException $e) { if ($testing) { throw new $e(); } $this->redirectToAdminSettings($e); } catch (\Exception $e) { if ($testing) { throw new $e(); } $this->redirectToError($e); } } else { throw new \Exception('Controller ' . $controller . ' not found'); } }
/** * @return PDO object, */ protected function connection() { if ($this->dbConnection == null) { $settings = \core\Loader::load('blogg\\model\\admin\\Settings'); $settings = $settings->getBloggSettings(); $this->dbConnection = new \PDO($settings->connectionString, $settings->dbUserName, $settings->dbPassword); if (\Config::DEBUG) { $this->dbConnection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } } return $this->dbConnection; }
<?php session_start(); define('DS', DIRECTORY_SEPARATOR); define('ROOT_DIR', dirname(__FILE__) . DS); define('ROOT_PATH', '/' . basename(dirname(__FILE__)) . '/'); require_once './core/definer.php'; \core\Loader::load('blogg\\model\\admin\\Settings'); //börja med att läsa in inställningar $router = \core\Loader::load('\\core\\Router'); $router->dispatch();