I have the code below where my variable $layout
loses it's value and I have no idea why, the layoutContent
is just a method that get the template of my website, then renderOnlyView
get the view supposed to render in that template
$layout = $this->layoutContent($layoutParams);
echo ($layout);
$view = $this->renderOnlyView($view, $params);
echo ($layout);
the whole method where the bug occurred
public function renderView(string $view, array $params = [], array $layoutParams = [])
{
$layout = $this->layoutContent($layoutParams);
echo ($layout);
$view = $this->renderOnlyView($view, $params);
echo ($layout);
$layout = str_replace('{{ title }}', $layoutParams['title'] ?? 'No title', $layout);
return str_replace('{{ body }}', $view, $layout);
}
and renderOnlyView
protected function renderOnlyView(string $view, array $params): ?string
{
foreach ($params as $key => $param) {
$$key = $param;
}
ob_start();
$viewFilePath = Application::$ROOT_DIR . "/views/$view.blade.php";
if (file_exists($viewFilePath))
include_once $viewFilePath;
else
echo "the view <b>$view</b> is not found";
return ob_get_clean();
}
layoutContent
just in case
public function layoutContent(array $params = [])
{
$layout = Application::$APP->controller->layout ?? 'main';
ob_start();
include_once Application::$ROOT_DIR . "/views/layout/$layout.layout.php";
$output = ob_get_clean();
foreach ($params as $key => $param) {
$output = str_replace('{{ ' . $key . ' }}', $param, $output);
}
return $output;
}
more infos: PHP Version: 7.4.3, system ubuntu 20.04
你错过了一个点,这个函数被调用了两次,第一次是在你渲染我的视图时,第二次是当你视图抛出一个错误并且在捕捉它并尝试显示时再次调用 renderLayout,导致 include_once 返回 null因为它已经包含了 xD
好吧,伙计们发现问题,我错过了一个点,这个函数被调用了两次,第一次是当我渲染我的视图时,第二次当我的视图抛出错误并在捕获它并尝试显示renderLayout
再次被调用,导致 include_once 返回 null,因为它已经包含在内:D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.