I'm confused by this as I can't find good documentation on how to do this, I have a jquery file called "index" located in my cakephp site folder at:
/app/webroot/js/index.js
I am trying to include it in my view with:
<?php
echo $javascript->link('index', false);
?>
but I get:
Fatal error: Call to a m开发者_C百科ember function link() on a non-object in /var/www/site1/app/views/uiemails/index.ctp on line 4
I have also put this in my layout's header:
<?php
//load the jQuery core
$javascript->link('jquery-1.4.2.min', false);
?>
I would appreciate any advice as to what I need to do to get this to work.. thanks
Edit
(I am using cakephp version 1.3.2 I have now tried:
putting this in my controller:
var $helpers = array('Form', 'Html');
then, in my view:
<?php
echo $this->html->script('index', false);
?>
but I still get:
Fatal error: Call to a member function script() on a non-object in /var/www/site1/app/views/uiemails/index.ctp on line 4
It looks like you're forgetting to include the Javascript helper in your controller. The syntax also depends on which version of CakePHP you're using. In 1.3.x, the Javascript helper has been deprecated and you would use the Html helper instead.
Your controller:
<?php
class BakeriesController extends AppController {
// not required if you're using the Html helper since it's included by default!
var $helpers = array('Form', 'Html', 'Javascript');
}
?>
Your view:
<?php echo $this->Javascript->link('whatever'); ?>
or with CakePHP 1.3.x:
<?php echo $this->Html->script('whatever'); ?>
And in your filesystem:
/app/webroot/js/whatever.js
The relevant documentation:
- http://book.cakephp.org/view/1096/Using-Helpers for how to include non-default helpers
- http://book.cakephp.org/view/349/Methods for using the 1.2.x Javascript helper
- http://book.cakephp.org/view/1589/script for using the 1.3.x Html helper to include scripts
Well, it seems that it works when I do it this way:
<?php
//load javascript file that will be specific to this view (page)
echo $html->script('index');
?>
I was confused (using $this->html->script) since other tutorials I found said to do this but apparently its incorrect, at least in the latest version of cakephp.
This is not right:
echo $this->html->script('index', false);
It should be:
echo $html->script('index', false);
You should also be careful not to include any javascript twice (e.g. in a layout and in a view).
echo $this->js->link('jquery-1.10.1.min');
Output:
<a href="/cake/posts/jquery-1.10.1.min" id="link-1138978643">/cake/posts/jquery-1.10.1.min</a>
Try this :
echo $this->Html->script('jquery-1.10.1.min');
Output:
<script type="text/javascript" src="/cake/js/jquery-1.10.1.min.js"></script>
精彩评论