【发布时间】:2014-12-10 04:08:39
【问题描述】:
我正在通过编写一个小测试应用程序来学习如何使用 Laravel。到目前为止,它进展顺利,我对框架的所有功能感到兴奋。但是,当我尝试应用特定的设计模式时,我总是碰壁。
我目前有一个设置,可以在基本控制器中使用默认值设置 html 标题、关键字和描述,然后可以选择在扩展它的子控制器中更改它们:
基础控制器
protected function setupLayout()
{
$this->layout->title = 'Wow website';
$this->layout->desc = 'Much technical';
$this->layout->keys = 'so html, very javascript';
$foot = View::make('mod.footerDefault');
$this->layout->footer = $foot;
}
儿童控制器
public function getContact()
{
$this->title = 'Contact page of contactness';
// Could also override the desc and keys if desired,
// but I'll just let them default here
$data = 'wow';
$view = View::make('main.home', $data)
->nest('vid', 'mod.video')
->nest('list', 'mod.list');
$this->layout->content = $view;
}
这都是肉汁,但是如果我有一个我与之交互的对象,例如菜单类,需要重新编译成 HTML 字符串怎么办? laravel 中是否有类似 setupLayout() 方法自动调用,但每次调用 View::make 方法的方法?
(更多示例...只是为了说明)
基础控制器
protected function setupLayout()
{
$this->menu = newMenu();
$this->menu->items[0] = ['contact-us', 'Contact us'];
$this->menu->items[1] = ['about-us', 'About us'];
$this->menuString = $this->menu->getString();
}
儿童控制器
public function getContact()
{
$this->menu->addItem(['log-out', 'Log out']);
// Now $this->menuString must be recalculated.
// Ideally want to avoid having to call a method from the base controller
// every time the child controller calls View::make
$view = View::make('main.home')
$this->layout->content = $view;
}
我知道有视图作曲家这样的东西,但我不认为这些是在 View::make() 上调用的。
【问题讨论】: