设置cakephp以使用JQuery

时间:2022-12-05 08:17:43

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?

我最近遇到了CakePhp作为一个优秀的框架,我目前正在移植我的网站蛋糕。在使用JQuery方面,目前推荐的包含javascript /访问javascript文件的方法是什么。做一点挖掘,有些人建议app / config中心位置......你们都有什么想法?

Thanks.

谢谢。

5 个解决方案

#1


11  

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

将包含jquery的javascript文件添加到/ app / webroot / js /。

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

然后在你的布局(/app/views/layouts/default.ctp)上,只需使用javascript助手加载你的javascript文件。

<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.

确保在您的应用上加载了javascript助手。在app_controller.php或单个控制器上。

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

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

#2


1  

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

添加jQuery的一种方法是将其添加到jpdelatorre建议的布局中。而不是

$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) :

所以现在编辑的jpdelatorre应该是这样的(在布局中,例如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:

这也意味着您不要在控制器中包含javascript助手。所以助手看起来像:

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

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

这是一个关于如何包含jQuery的cakePHP教程的链接。

#3


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.

如果你想在你的所有页面上都包含jquery,我只想在你的应用程序使用的布局中添加一行来包含它们。您可以使用javascript帮助程序,或者只使用标准HTML将其包含在标记中。

#4


0  

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

就我而言,我不得不继续并指定JQuery文件的版本,如下所示:

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

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

Otherwise it just couldn't work!

否则它就行不通!

#5


0  

To link javascript in CakePHP 3.x use

要在CakePHP 3.x中使用链接javascript

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

The above code will yield

上面的代码将产生

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

#1


11  

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

将包含jquery的javascript文件添加到/ app / webroot / js /。

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

然后在你的布局(/app/views/layouts/default.ctp)上,只需使用javascript助手加载你的javascript文件。

<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.

确保在您的应用上加载了javascript助手。在app_controller.php或单个控制器上。

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

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

#2


1  

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

添加jQuery的一种方法是将其添加到jpdelatorre建议的布局中。而不是

$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) :

所以现在编辑的jpdelatorre应该是这样的(在布局中,例如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:

这也意味着您不要在控制器中包含javascript助手。所以助手看起来像:

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

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

这是一个关于如何包含jQuery的cakePHP教程的链接。

#3


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.

如果你想在你的所有页面上都包含jquery,我只想在你的应用程序使用的布局中添加一行来包含它们。您可以使用javascript帮助程序,或者只使用标准HTML将其包含在标记中。

#4


0  

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

就我而言,我不得不继续并指定JQuery文件的版本,如下所示:

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

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

Otherwise it just couldn't work!

否则它就行不通!

#5


0  

To link javascript in CakePHP 3.x use

要在CakePHP 3.x中使用链接javascript

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

The above code will yield

上面的代码将产生

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