1

I've recently come across CakePhp as an excellent framework and am currently porting over my site to cake. In terms of using JQuery, what is the currently recommended method for including the javascript / accessing the javascript files. Doing a little digging, some people have suggested a central place in app/config ... what do you all think?

Thanks.

5 Answers 5

11

Add your javascript files including jquery to /app/webroot/js/.

Then on your layout (/app/views/layouts/default.ctp), just use the javascript helper to load your javascript files.

<head>
<title><?php echo $title_for_layout; ?></title>
<?php echo $javascript->link('jquery'); ?>
...
</head>

Make sure javascript helper is loaded on your app. Either on your app_controller.php or individual controllers.

var $helpers = array('Html', 'Form', 'Javascript');

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget you can link jQuery from Google too! code.google.com/apis/ajaxlibs/documentation/index.html#jquery
1

One way to add jQuery is to add it to layout as suggested by jpdelatorre. Except instead of

$javascript->link('jquery');

use

$this->Html->script('jquery');

So now jpdelatorre edited should look like this (within the layout e.g. app/View/Layouts/default.ctp) :

<head>
<title><?php echo $title_for_layout; ?></title>
<?php echo $this->Html->script('jquery'); ?>
...
</head>

This also means you do not to include the javascript helper in your controller. So the helper would look like:

var $helpers = array('Html', 'Form');

Here is a link to the cakePHP tutorial on how to include jQuery.

Comments

0

If you want jquery included on all of your pages, I would just put a line to include them in the section in the layouts used by your app. You can use the javascript helper, or just use standard HTML to include it with a tag.

Comments

0

In my case, I had to go on further and specify the version of the JQuery file, like this:

echo $this->Html->script('jquery-1.8.3');

Otherwise it just couldn't work!

Comments

0

To link javascript in CakePHP 3.x use

echo $this->Html->script('scripts');

The above code will yield

<script src="/js/scripts.js"></script>

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.