/** * Build the global layout with all children contents * * @param array $params An array of the parameters passed for the view parsing * @return string Returns the view file content rendering */ public function renderLayout(array $params = array()) { $params = array_merge($this->getParams(), $params); $children = array(); foreach ($this->_children as $name => $item) { if (Helper::classImplements($item, AppKernel::getApi('template'))) { $children[$name] = $item->render(); } else { $children[$name] = $item; } } $params = array_merge($params, $children); $params['children'] = $children; return $this->render($this->getView(), $params); }
/** * @param null|string $controller * @param null|string $action * @param array $arguments * @return string * @throws \MVCFundamental\Exception\InternalServerErrorException */ public function callControllerAction($controller = null, $action = null, array $arguments = array()) { $this->boot(); $controller_name = $controller; if (empty($controller)) { $controller = $this->getOption('default_controller_name'); } if (empty($action)) { $action = $this->getOption('default_action_name'); } if (!empty($controller)) { if (!is_object($controller)) { $ctrl = $this->get('locator')->locateController($controller); if (!empty($ctrl)) { $controller = new $ctrl(); } else { throw new InternalServerErrorException(sprintf('Unknown controller "%s"!', $controller_name)); } } $arguments = array_merge(Helper::getDefaultEnvParameters(), $arguments); $action = $this->get('locator')->locateControllerAction($action, $controller); if (!method_exists($controller, $action) || !is_callable(array($controller, $action))) { throw new InternalServerErrorException(sprintf('Action "%s" in controller "%s" is not known or not callable!', $action, $controller_name)); } $result = Helper::fetchArguments($action, $arguments, $controller); // result as a raw content: a string if (is_string($result)) { return $result; // result as an array like ( view_file , params ) } elseif (is_array($result)) { $view_file = $this->get('template_engine')->getTemplate($result[0]); if (!empty($view_file)) { return $this->render($view_file, isset($result[1]) && is_array($result[1]) ? array_merge($arguments, $result[1]) : $arguments); } // a reponse object } elseif (is_object($result) && ($ri = AppKernel::getApi('response')) && $result instanceof $ri) { $this->set('response', $result); } } return ''; }
/** * @param array $arguments * @return mixed */ public function getDefaultLayout(array $arguments = array()) { $view = FrontController::getInstance()->getOption('default_layout'); $class = FrontController::getInstance()->getOption('default_layout_class'); if (class_exists($class) && Helper::classImplements($class, AppKernel::getApi('layout'))) { $layout = new $class($arguments); $layout->setLayout($view); return $layout; } return null; }