我不确定每View
Controller
的优势是什么?我读过的关于Views
的任何内容都没有清楚地显示或说明为什么你应该View
每Controller
。
目前我有这样的事情(缩短为主要方法):
interface ViewInterface {
public function render();
}
class View implements ViewInterface {
private $template;
public function __construct($template = NULL) {
if($template !== NULL) {
$this->setTemplate($template);
}
}
public function render() {
ob_start();
require(PATH_TEMPLATES . $this->template . '.php');
return ob_get_clean();
}
}
class CompositeView implements ViewInterface {
private $views;
public function attachView(ViewInterface $view) {
$this->views[] = $view;
return $this;
}
public function render() {
$output = '';
foreach($this->views as $view) {
$output .= $view->render();
}
return $output;
}
}
假设我要呈现隐私政策页面,我可以这样做
$header = new View('common/header');
$body = new View('privacy-policy');
$footer = new View('common/footer');
$compositeView = new CompositeView();
$compositeView->attachView($header)
->attachView($body)
->attachView($footer);
$compositeView->render();
这有什么问题?每个View
单独Controller
会有什么好处?
任何建议都会非常感谢。