public static function route(\Request $request) { $method = $request->getRequestMethod(); $req = $request->getRequestedUri(); $routes = new \Config("routes"); $routes = $routes->{$method}; foreach ($routes as $url => $handler) { $url = preg_replace("|\\:([a-z0-9\\_\\-]+)|iu", "(?P<\$1>.*)", $url); if (preg_match("|{$url}|iu", $request->getRequestedUri(), $params)) { list($controller, $method) = explode("::", $handler); $request->setParams($params); break; } } if (!isset($controller)) { $controller = "Controller\\BadRequest"; $method = "output404"; } $controller = new $controller(); $auth = new Authentication\Auth(); if ($controller instanceof Controller\AbAuthController && !$auth->isLoggedIn()) { return array($controller, "checkLogin"); } return array($controller, $method); }
public function checkLogin(\Request $req, \Response $res) { //check if login is required first. $auth = new \Authentication\Auth(); if (!$auth->isLoggedIn()) { $res->json(array("flash" => "You must login!"), "401"); } }