2013-03-24 28 views
0

我奮力調用組件在一個名爲組件用自己的行動的另一個模塊:: functionnameSymfony1.2組件問題

例如

模塊/用戶/動作/的actions.class.php有像getUserName()函數 我打電話userActions ::在同一個模塊的組件getUserName()函數,它的做工精細即

文件:模塊/用戶/動作/ components.class.php

public function executeShowUsername() { 
$this->userName =userActions::getUserName(); 
} 

組件模板(module/user/templates/_showUsername.php) echo $ userName; (細工作)

現在,問題是: 當包括( '用戶', 'showUserName')成分中的其他模塊等 文件:模塊/生成/模板/ indexSuccess.php的

<?php include_component('user', 'showUserName'); ?> 

我出現錯誤:

Fatal error: Class 'userActions' not found in /myproject/apps/frontend/modules/user/actions/components.class.php on line 86

我該如何解決? 我們不能調用其他模塊組件如果調用函數一樣 userActions :: getUserName()

回答

0

如果您正在使用的用戶會話時,您可能只需要直接訪問它在佈局模板(見用戶會話在http://symfony.com/legacy/doc/gentle-introduction/1_4/en/06-Inside-the-Controller-Layer部分):

<?php echo $sf_user->getAttribute('nickname') ?>

否則,將變量傳遞到部件(請參見組件部分中http://symfony.com/legacy/doc/gentle-introduction/1_4/en/07-Inside-the-View-Layer):

[清單7-13 - 將參數傳遞給COMPONE NT和它的模板]

// Call to the component 
// rather than calling userActions::getUserName() in the component.class 
// put your code from userActions::getUserName() into the model (or other accessible place) 
// then call the component 
<?php include_component('user', 'showUserName', array('MyUserName' => 'myuser')) ?> 

// In the component itself 
echo $this->MyUserName; 
/* results in */ => 'myuser' 

// In the _show_user_name.php partial 
echo $MyUserName; 
/* results in */ => 'myuser' 
+0

我不工作與會議,在上述例子中,我簡單地實際上funtion到getUserName(),但它給我很大的幫助,如果我可以在任何其他模塊中使用showUserName組件。 此外,我不清楚爲什麼我需要從全局訪問類文件中移動代碼的方式,我想在symfony1.2中支持的方式嗎? – user001 2013-03-26 01:22:11

+0

我想你調用的方式在引用actions.class的組件中獲取userName()不適用於框架的設計。這就是爲什麼我認爲如果將getUserName()函數放入模型中,您可以在組件中訪問它,並將其包含在其他模塊中。希望這可以幫助。 – 2013-03-27 00:26:34