/** * Method to load environment variables */ protected function loadDotEnv() { if (!getenv('APP_ENV')) { $dotenv = new \Dotenv\Dotenv(__DIR__ . '/../../'); $dotenv->load(); } }
public function start($root = '') { $root = $root ?: dirname(__DIR__); $this->container['root_path'] = $root; if (!isset($this->container['config_path'])) { $this->container['config_path'] = $root . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'config'; $configFile = $this->container['config_path'] . DIRECTORY_SEPARATOR . 'main.php'; if (file_exists($configFile)) { $this->container['config'] = (include $configFile); } } if (!isset($this->container['controller_path'])) { $this->container['controller_path'] = $root . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'controllers'; } if (!isset($this->container['model_path'])) { $this->container['model_path'] = $root . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'models'; } if (file_exists($root . DIRECTORY_SEPARATOR . '.env')) { $env = new \Dotenv\Dotenv($this->container['root_path']); $env->load(); } // Fetch method and URI from somewhere $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) { foreach ($this->routes as $route) { $r->addRoute($route['method'], $route['pattern'], $route['target']); } }); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); if ($routeInfo[0] == \FastRoute\Dispatcher::NOT_FOUND) { $controllerFile = $this->container['controller_path'] . $uri . '.php'; if (file_exists($controllerFile)) { $app = $this; $this->container['method'] = $httpMethod; $render = function ($controller) use($app) { include $controller; }; echo $render($controllerFile); return; } echo '404 Not Found'; return; } switch ($routeInfo[0]) { case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; echo '405 Method Not Allowed'; break; case \FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $this->container['method'] = $httpMethod; $this->container['vars'] = $routeInfo[2]; $controllerFile = $this->container['controller_path'] . DIRECTORY_SEPARATOR . $handler; if (file_exists($controllerFile)) { $app = $this; $render = function ($controller) use($app) { include $controller; }; echo $render($controllerFile); } else { echo '404 Not Found'; return; } } }