Reputation: 6884
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.
Upvotes: 1
Views: 10607
Reputation: 1396
To link javascript in CakePHP 3.x use
echo $this->Html->script('scripts');
The above code will yield
<script src="/js/scripts.js"></script>
Upvotes: 0
Reputation: 1
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!
Upvotes: 0
Reputation: 143
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.
Upvotes: 1
Reputation: 3593
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');
Upvotes: 11
Reputation: 4230
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.
Upvotes: 0