好的,首先是免責聲明。我正在研究我的第一個Sf2項目,並通過現有的sf1項目進行工作,並將其重構爲Sf2,以瞭解所有抽屜已移到的位置。東西在樹枝和子控制器之間吃我的東西
現在發生了一些奇怪的事情,它讓我撕掉了最後一根頭髮。
--- TL; DR:摘要---
我渲染從樹枝模板控制器和傳遞數組通過到子控制器。其中一個數組值是一個對象,但該對象在它到達控制器之前正在消失。
我已經追蹤它通過Symfony2流盡可能我和可以跟蹤對象是完整的。然後我迷路了,但是在Symfony \ Component \ HttpKernel \ Fragment \ InlineFragmentRenderer :: render()和我的(子)控制器之間的某個地方,對象消失了。
這是一個功能,一個bug或只是我瘋了嗎?
---詳細---
我有一個處理實體對象(分配給樹枝變量「集合」),其含有與實體如的ImageItem和linkedPageItem)樹枝模板。
我的實體已經設置爲擴展一個實現ArrayAccess的基類,因此我可以使用數組符號訪問這些值是否對象是水合的。 (按照此處列出的選項#1方法:http://docs.doctrine-project.org/en/latest/cookbook/implementing-arrayaccess-for-domain-objects.html)。
在樹枝中,我有一條這樣的線,它將一些對象傳遞給一個子控制器來渲染一個圖像塊。 renderImage子控制器在渲染它自己的小枝塊之前管理所有事物的清理。
下面是主要的樹枝文件中的代碼:
{# blockLinks.html.twig #}
...
{{- render(controller(
'MyBundle:Helper:renderImage',
{
'image': collection['imageItem'],
'options': {
'html_options': { 'class': 'foo' },
'link_options': { 'class': 'bar' },
'link_url': collection['item_link_url'],
'link_page': collection['linkedPageItem']
}
}
)) -}}
...
然而,renderImage子控制器永遠不會接收的$選項[「link_page」]的值。
這很奇怪,所以我更深入地挖掘了它。我發現了一個名爲blockLinks.html.twig的PHP文件,並將其深入研究。
在那裏,我們發現:
// /app/cache/dev/twig/01/30/lotsofnumbers.php
...
echo $this->env->getExtension('http_kernel')->renderFragment($this->env->getExtension('http_kernel')->controller(
"CaponicaFdsWebsiteBundle:Helper:renderImage",
array(
"image" => $this->getAttribute($_collection_, "imageItem", array(), "array"),
"options" => array(
"html_options" => array("class" => "foo"),
"link_options" => array("class" => "bar"),
"link_url" => $this->getAttribute($_collection_, "item_link_url", array(), "array"),
"link_page" => $this->getAttribute($_collection_, "linkedPageItem", array(), "array")
)
)
));
...
OK,看起來不錯。
爲了弄清楚發生了什麼事,我做了\Doctrine\Common\Util\Debug::dump($this->getAttribute($_collection_, "linkedPageItem", array(), "array"));
,它報告說這確實返回一個Page對象。
即它報告是這樣的:
object(stdClass)[874]
public '__CLASS__' => string 'MyBundle\Entity\Page'
...
從樹枝編譯PHP文件緩存
開始 - > Symfony的\橋\嫩枝\延期\ HttpKernelExtension :: renderFragment()
- >的Symfony \分量\ HttpKernel \片段\ FragmentHandler ::渲染()
- >的Symfony \分量\ HttpKernel \片段\ InlineFragmentRenderer ::渲染()
此時對象依然完好 (傾倒看到$ subRequest->屬性 - > GET ('options')就在調用$ this-> kernel-> handle($ subRequest,Ht tpKernelInterface :: SUB_REQUEST,false); InlineFragmentRenderer的第62行)
我不確定接下來會發生什麼。我以爲它去了[\ vendor \ symfony \ symfony \ src] Symfony \ Bundle \ FrameworkBundle \ HttpKernel,但我似乎無法跟蹤任何活動。
但是,如果我看在renderImage控制器和轉儲這樣的選項:
// MyBundle\Controller\HelperController.php
public function renderImageAction($image, $options = array()) {
print_r(\Doctrine\Common\Util\Debug::dump($options)); // I'm sure there's a 1337 way to use the logger/toolbar for this, I'll figure that out some other day
...
}
我得到這個:
array (size=2)
'html_options' =>
array (size=1)
'class' => string 'foo' (length=3)
'link_options' =>
array (size=1)
'class' => string 'bar' (length=3)
換句話說,在 'link_page' 對象已經從選項中刪除 - 飢餓(而且非常可怕)的東西似乎潛伏在Symfony的深處並吃掉了我的東西!
這是功能嗎? (你不能將一個數組內的對象傳遞給子請求......也許這會激怒超級嚴格的MVC神)
或者它是一個錯誤? (有東西在吃東西不應該?)
或者我剛剛得到了這個特定棒的錯誤結局?
我希望有人能幫助,之前我失去的頭髮最後股...
Ç
是否使用'{{ - '或'{{'在樹枝模板? – cheesemacfly 2013-04-10 16:31:19
我在顯示的部分使用了'{{-'(模板的其他部分不使用空格控件) – caponica 2013-04-10 16:53:37
這可能是一個愚蠢的問題,但爲什麼你使用'{{-'而不是'{{ {'?那裏有什麼? – cheesemacfly 2013-04-10 16:55:16