2012-12-08 35 views
0

我只是想知道如何在CakePHP中使用$this->set()方法來使用/定義我自己的函數?我想要做這樣的事情...

AppController.php

<?php 
    function checkSetup() { 
     if ($this->Auth->user('setup') == 'notcomplete') { return true; } 
    } 

    $this->set('isSetup', checkSetup()); 
?> 

,然後我將能夠訪問並調用它在我的視圖文件:

<?php if ($isSetup): ?> 
You haven't setup your profile yet! 
<?php endif; ?> 

我我已經嘗試過了,但是顯然這不起作用,因爲我得到了一個巨大的致命錯誤。有關我如何做到這一點的任何想法/建議?

回答

1
$this->set('isSetup', checkSetup()); 

該行需要在某個函數內才能被調用。想必你想在你的應用程序控制器的beforFilter - 這樣的事情:

<?php 

App::uses('Controller', 'Controller'); 

class AppController extends Controller { 

    function beforeFilter() { 
     $this->set('isSetup', checkSetup()); 
    } 

    function checkSetup() { 
     if ($this->Auth->user('setup') == 'notcomplete') { return true; } 
    } 

} 

?> 
+0

感謝您的幫助,我沒有意識到它必須在一個函數。隊友的歡呼聲 ;) –