2010-03-23 74 views
0

您好,我有兩個問題:

(1)它是在引導文件創建全局自定義函數的最佳實踐?有沒有更好的地方來存放它們?

(2)我無法使用下面的代碼行中位於我的bootstrap.php文件我的自定義功能:

$url = $ajax->link ('Delete', array ('controller' => 'events', 'action' => 'delete', 22), array ('update' => 'event'), 'Do you want to delete this event?'); 
    echo $url; 

我收到以下錯誤:

Notice (8): Undefined variable: ajax [APP\config\bootstrap.php, line 271] 

Code 

} 

function testAjax() { 

      $url = $ajax->link ('Delete', array ('controller' => 'events', 'action' => 'delete', 22), array ('update' => 'event'), 'Do you want to delete this event?'); 

testAjax - APP\config\bootstrap.php, line 271 
include - APP\views\event\queue.ctp, line 19 
View::_render() - CORE\cake\libs\view\view.php, line 649 
View::render() - CORE\cake\libs\view\view.php, line 372 
Controller::render() - CORE\cake\libs\controller\controller.php, line 766 
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 211 
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 181 
[main] - APP\webroot\index.php, line 91 

然而,按預期工作,如果我把,我認爲,同樣的代碼:

<a onclick=" event.returnValue = false; return false;" id="link1656170149" href="/shout/events/delete/22">Delete</a> 

請幫助:)

在此先感謝!

回答

1
  1. 這取決於:如果它是一個完整的通用函數,可以從應用程序的任何地方訪問,比是的。否則,我會把它放在你想要使用它的父類中(app_model,app_controller)
  2. $ajax是一個幫助類,它不能從你的引導文件中訪問。你需要在助手中加入助手,這是從那裏沒有意義的地方放置功能