public function find($query) { $pdo = Service::get('pdo'); $stmt = $pdo->prepare($query); $stmt->execute(); return $stmt->fetch($pdo::FETCH_LAZY); }
public static function render($view, $params = array()) { $config = Service::get('app')->config; $controller = strtolower($config['controller']); $dir = preg_replace('/\\w+$/', '', __DIR__); $view_path = $dir . 'views/' . $controller . '/' . $view; $layout_path = $dir . 'views/layout/' . $config['layout']; $content = self::extract($view_path, $params); return self::extract($layout_path, array('content' => $content, 'title' => $params['title'])); //rendering into main template }
public function run() { try { $route = Router::parseURI($_SERVER['REQUEST_URI']); $controller = 'Lib\\Controller\\' . $route['controller'] . 'Controller'; $action = $route['action'] . 'Action'; $args = $route['params']; $this->config['controller'] = $route['controller']; // for render $controllerReflection = new \ReflectionClass($controller); if ($controllerReflection->hasMethod($action)) { $reflectionMethod = new \ReflectionMethod($controller, $action); $response = $reflectionMethod->invokeArgs(new $controller(), $args); $response->send(); } else { throw new \Exception('The action "' . $action . '" not found'); } } catch (\Exception $e) { Service::get('session')->set('error', $e->getMessage()); header('Location: /error'); } }
public static function parseURI($uri) { $uri = preg_replace('/\\?.+$/', '', $uri); $uri_explode = explode('/', $uri); array_shift($uri_explode); $count_uri = count($uri_explode); $config = Service::get('app')->config; $controller = $config['defaultController']; if ($uri == '/') { $action = $config['defaultAction']; } elseif ($count_uri == 1) { $action = array_shift($uri_explode); } elseif ($count_uri > 1) { $action = array_shift($uri_explode); $params = $uri_explode; } else { throw new \Exception("Routing doesn't found."); } $params = isset($params) ? $params : array(); $route = array('controller' => $controller, 'action' => $action, 'params' => $params); return $route; }
protected function addFilm($title, $release_year, $format, $stars) { if ($release_year > date('Y')) { throw new \Exception('Invalid year!'); } if (empty($title) || empty($release_year) || empty($format) || empty($stars)) { throw new \Exception('Invalid data!'); } $pdo = Service::get('pdo'); $query = "INSERT IGNORE INTO `format` (`format`) VALUES ('{$format}')"; $pdo->prepare($query)->execute(); $query = "INSERT INTO `film` (title, release_year, format_id) \n\t\t\tVALUES (\n\t\t\t\t'{$title}', \n\t\t\t\t{$release_year}, \n\t\t\t\t(\n\t\t\t\t\tSELECT `format`.`id` FROM `format` \n\t\t\t\t\tWHERE `format`.`format`='{$format}'\n\t\t\t\t)\n\t\t\t)"; $pdo->prepare($query)->execute(); foreach ($stars as $star) { $star = trim($star); $query = "INSERT IGNORE INTO `star` (`name`) VALUES ('{$star}')"; $pdo->prepare($query)->execute(); $query = "INSERT INTO `film_star` (`film_id`, `star_id`) \n\t\t\t\tVALUES ( \n\t\t\t\t\t(SELECT `id` FROM `film` ORDER BY `id` DESC LIMIT 1), \n\t\t\t\t\t(SELECT `id` FROM `star` WHERE `name`='{$star}') \n\t\t\t\t)"; $pdo->prepare($query)->execute(); } }