我有一個組件,一直在愉快地構建和呈現菜單一段時間。現在我必須提供一個共享所有相同邏輯的特殊情況,但需要在已存在的情況下進行一些工作。我想要做的是創建一個新的組件動作,它將執行必要的預處理,通過共享邏輯來完成計算側,然後通過現有模板部分渲染(當所有事情都說完之後,它仍然是一個像任何其他 - 它只需要更多的工作來構建它)。Symfony:在兩個組件動作之間共享部分
不幸的是,我找不到任何這樣做的方式。
這裏是高層次的文件/代碼崩潰,我現在所擁有的:
#
# navigation/actions/components.class.php
#
public function executeMenu() {
/**
* This method runs most of the menus and does most of the work
* that's required of the special case.
*
* Once complete, of course, it renders through navigation/templates/_menu.php
*/
}
public function executeSpecialMenu() {
/**
* Do some preparatory work and delegate to executeMenu()
* to finish up and render the menu. I'd like this action
* to render through the _menu.php partial as well.
*/
}
#
# templates/layout.php
#
<?php include_component('navigation', 'menu', array('menu' => 'Entity Type')) ?>
/** SNIP */
<?php include_component('navigation', 'SpecialMenu') ?>
任何輸入將不勝感激。
解決了功能級別的問題,這是我最初實施的解決方案。對我來說,這感覺真的很火,所以我希望那裏有更多的手術方案。不過,我確實選擇了一個直接的'include()',這樣變量就可以運行了,而且每次添加/刪除變量時,我都不必更改2個文件。 – 2010-02-25 15:04:29
是的,'sfComponents'沒有提供一個簡單的方法來實現這個功能,所以你堅持使用你的'include()',這是最簡單也是最有前途的方法。 – 2010-02-25 15:25:04