【问题标题】:Cakephp 2.0: Htmlhelper throwing array_merge() error when using Html->script()Cakephp 2.0:使用 Html->script() 时 Htmlhelper 抛出 array_merge() 错误
【发布时间】:2012-01-03 17:21:27
【问题描述】:

我正在使用以下代码来实现一些 jquery-ui 功能:

<?php  $this->Html->script(array(
                              'jquery-ui-1.8.16.custom.min.js',
                              'jquery-1.6.2.min.js'
                                ), 
                           null,
                           array(
                                'inline'=>'false', 
                                'once'=>'true'
                                 ) 
                           ); 
?>

<script>
    jQuery(document).ready(function(){
        $('.cv .collapsable').click(function() {
            $(this).next().toggle('slow');
            return false;
        }).next().hide();
    });
</script>

目标元素/类是一个元素和嵌套元素。然而,jquery 功能正在工作,但代价是 php array_merge() 错误,如下所示:

Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]
Warning (2): array_merge() [function.array-merge]: Argument #2 is not an array [CORE/Cake/View/Helper/HtmlHelper.php, line 478]

我做错了吗?这是一个错误吗?我尝试将 &lt;script&gt; 标签添加到 default.ctp 布局,但也无法使 URL 正常工作。我已经用 cakephp 1.3 完成了这个,所以我对我现在遇到的问题有点困惑。任何帮助将不胜感激。

更新

这是我的完整代码: PHP:

<?php  $this->Html->script(
    array(
       'jquery-1.6.2.min'
    ),
    null,
    array(
        'inline'=>'false', 
    ) 
);  ?>

jquery:

<script>
    jQuery(document).ready(function() {
    $('div.collapsable').click(function() {
        $(this).next().slideToggle("slow");
        return false;
    });
});
</script>

我已经删除了 jquery-ui 脚本来简化事情——当我这样做时,我现在只会抛出两个“array_merge”错误。尽管如此,如果我删除“null”——它甚至不应该存在,因为该函数只接受两个参数——我会失去所有功能(错误也会消失)。

但是事情变得更奇怪了!

我查看了 cake->core->lib->helper->htmlhelper 并检查了函数;它有一行检查$options 数组是否为布尔值。如果我改为使用此代码:

<?php  $this->Html->script(
        array(
            'jquery-1.6.2.min'
        ),
        false
);      ?>

我失去了指定inline = trueonce = true 的能力,但是我至少获得了完整的功能而没有array_merge() 错误。这是 2.0 的错误吗?

【问题讨论】:

    标签: cakephp html-helper cakephp-2.0


    【解决方案1】:

    根据the documentation page$this-&gt;Html-&gt;script() 仅接受 2 个参数。删除null,你应该没问题。

    此外,您不应在脚本文件名的末尾包含 .js

    您的代码应如下所示:

    <?php
    $this->Html->script(
        array(
           'jquery-ui-1.8.16.custom.min',
           'jquery-1.6.2.min'
        ),
        array(
            'inline'=>'false', 
            'once'=>'true'
        ) 
    ); 
    ?>
    

    【讨论】:

    • 所以我尝试了,最初,你在这里有什么。奇怪的是:API 中给出的示例显示了“null”参数,而您共享的文档链接当然根本没有指示第三个参数。然而,如果我 包含它 cakephp 将不会打印脚本,而当 null 参数 is 存在时,它可以工作,但会出现我最初提到的错误问题。我感到困惑、迷失和沮丧。 :P 感谢您提供有关文件扩展名的提示。
    • 你试过不带文件扩展名吗?还可以尝试删除“once”参数(只是为了好玩)。我几乎每天都使用这个代码,而且我从不包含null - 只是上面的代码(但我不使用'once')。
    • 复制/粘贴您正在使用的确切代码(删除 null 和 .js 扩展名)。只需将其作为“编辑”添加到您的问题中,或者如果您没有权限,请将其添加为您的问题的评论bin.cakephp.org
    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多