I get a fatal error when trying to use the javascript helper in cakePHP. I am following this document, I have added jquery to my default layout:
<?php
<head>
echo $this->Html->script('jquery');
....
</head>
<body>
.....
echo $this->Js->writeBuffer(); // Write cached scripts
</body>
?>
I have also added the helper to my array of helpers in my controller:
<?php
public $helpers = array('Js' => array('Jquery'), 'Html', 'Form');
When I open the page with just these set it is fine and works, however if I add these lines of code to this function and then refresh the page:
public function index()
{
....
$this->Js->get('#draggable');
$this->Js->draggable();
}
Cake produces the error Error: Call to a member function get() on a non-object.
Any ideas as to what I am doing wrong?
Helpers are meant to be used in views, not in controllers, hence the error. Move the code from your
index()method to theindexview and the error should disappear.