0

I have problem when I want to use $javascript->link('prototype') in the default.ctp layout. It returns:

Undefined variable: javascript [APP\views\layouts\default.ctp, line 6]

I also added this code into app_controller.php:

<?
    class AppController extends Controller {
        var $Helpers = array('Html','Javascript','Ajax','Form');
    }
?>

The file prototype.js is already in webroot/js folder.

Where is the problem?

1
  • 1
    Which version of CakePHP? JavascriptHelper is deprecated from 1.3 on and it's probably not even shipped with 2.0. Commented Jul 26, 2011 at 16:49

4 Answers 4

1

I have had this problem many times. It's usually either caused by the controller code being overwritten somewhere or some weirdness happening with Cake's automagic stuff. If you remove all of your helpers and then add them one by one it will probably work eventually.

Another perfectly valid way of generating JavaScript links is by using the following which doesn't access the $javascript variable:

echo $html->script(array('prototype'));
Sign up to request clarification or add additional context in comments.

2 Comments

the array() should not be needed, and if he can't access Javascript Helper, I don't think the Html Helper would be different.
oh thank's! the helpers variable was overwritten in another place.
1

It has to be $helpers instead of $Helpers.

Comments

1

You just open the error console of the Firefox browser (shortcut key ctrl+shift+j).

  • Find the error and click on it.
  • After clicking, you will see the head portion.
  • Note the location of the JavaScript file (*.js) which you want to locate (you will see the location is not correct).
  • Cut the JavaScript file from webroot and paste it in given location of head block.

Example:
This will show on error console. Map_demo is my project, and in its place your project name will display:

<script type="text/javascript" src="/map_demo/js/test.js"></script>

  • Cut the JavaScript file from webroot
  • Make the JavaScript folder in your project application folder, /map_demo/js
  • Paste test.js (your script file) here

Now your JavaScript function will work.

Comments

0

Just in case somebody else comes across this bug/issue: it was also happening to me, until I commented out the line $session->flash(); in my default layout. Realising that the error was being caused by flash messages, I went back to the controller and noticed that I was using separate layouts for flash messages (e.g. 'message_alert') and that those layouts didn't actually exist in the view folder!

So remember, errors like this could mean that a file is not defined yet. Best of luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.