controllerDirectory = Base::load()->getBilling(); $this->init(); } /** * Initialize method */ public function init() { } /** * Try to resolve current controller by given params * * @param $controller * @param $action * @return mixed * @throws \Exception */ public function getController($controller, $action) { $this->action = empty($action) ? $this->defaultAction : $action; $this->controllerName = lcfirst($controller); try { $partsOfController = explode('-', $controller); $controllerClass = ''; foreach ($partsOfController as $part) { $controllerClass .= ucfirst($part); } $controllerClass .= 'Controller'; $this->controller = '\\App\\Controller\\'.$this->controllerDirectory.'\\'.$controllerClass; if (!class_exists($this->controller)) { throw new Exception('Undefined page'); } $model = new $this->controller; $model->controller = $this->controller; $model->controllerName = $this->controllerName; $model->action = $this->action; $model->setView(); return $model; } catch(\Exception $e) { throw new Exception($e->getMessage()); } } /** * Set view object */ public function setView() { $this->_view = new View($this->controllerName); } /** * Get View object * * @return View */ public function getView() { return $this->_view; } /** * Run controller action * * @throws \Exception */ public function runAction() { $actionMethod = lcfirst($this->action) . 'Action'; if(!method_exists($this, $actionMethod)) { throw new Exception('Undefined controller action "'.$this->action.'"'); } $method = new \ReflectionMethod($this->controller, $actionMethod); $method->invoke($this); } /** * Render view file with layout * * @param string $view * @param array $values * @param bool $output * @return string */ public function render($view = 'index', $values = array(), $output = true) { return $this->_view->render($view, $values, $output); } /** * Render view file * * @param string $view * @param array $values * @param bool $output * @return string */ public function renderPartial($view, $values, $output = true) { return $this->_view->renderPartial($view, $values, $output); } }