CakePHP: Custom Function in bootstrap that uses $ajax->link not working
Hello I have two questions:
(1) Is it best practice to create global custom functions in the bootstrap file? Is there a better place to store them?
(2) I am unable use the following line of code in my custom function located in my bootstrap.php file:
$url = $ajax->link ( 'Delete', array ('controller' => 'events', 'action' => 'delete', 22 ), array ('update' => 'event' ), 'Do you want to delete this event?' );
echo $url;
I receive the following error:
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.p开发者_JAVA百科hp, 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
However it works as intended if I place that same code in my view:
<a onclick=" event.returnValue = false; return false;" id="link1656170149" href="/shout/events/delete/22">Delete</a>
Please help :)
Thanks in advance!!
- That depends: If it's a complete generic function that could be accessed from everywhere in your app, than yes. Otherwise I would place it in the parent-class from where you want to use it (app_model, app_controller)
$ajax
is a helper class, that cannot be accessed from your bootstrap file. You would need to inculde the helper in the bootstrap, and that is the point from where it doesn't make sense to place the function there
Don't do it in bootstrap - no good place for that.
If You want to have this url on every page - put it in Your layout (http://book.cakephp.org/view/96/Layouts)
精彩评论