如何使用zend中公共目录下定义的js文件中的配置文件(或变量)

How to use configuration files(or variables) in js file which are defined under public directory in zend

本文关键字:文件 js 配置文件 变量 下定义 zend 何使用      更新时间:2023-09-26

我想在我的一些js文件中使用一些重要的变量,比如fb、twitter访问密钥,所以我在zend的config文件夹下定义了一个config.ini文件。我可以使用INI阅读器读取所有控制器和模型中的这些配置变量,但我不知道如何读取js文件中的这些变量。有没有其他方法可以为js文件定义配置变量?我的js文件位于公用文件夹下。为了在任何模块的index.phtml中读取这些变量,我使用视图模型从控制器返回这些变量,然后使用php读取phtml文件中的这些变量。有没有更好的方法来读取phtml文件中的这些可用项?

$reader = new Ini();
$data   = $reader->fromFile('config/config/config.ini');

我使用这段代码来读取config.ini文件,它运行良好。

使用Ajax

ZF2(PHP)侧:

  • 创建新的控制器/Ajax
    • 将其设置为空布局
    • 启用Json战略
    • 向Ajax cotroller添加新操作,例如AjaxAction
    • 编写代码

公共函数AjaxAction(){

//maybe create service for that?

    $reader = new Ini();

        $reader = new Ini();
        $data = $reader->fromFile('config/config/config.ini');
        return new JsonModel(['Data' => $data]);
    }

Jquery

jQuery(document).ready(function($){
var url = "http://urlToyourSite.com/ajax/ajax";
$.get(url).done(function(databack){
var data = databack.Data(); // here is your ini object
});
});

插入数据以布局

  • 创建新控制器/Ini
    • 向Ajax cotroller添加新操作,例如AddToLayoutAction
    • 编写代码

公共函数__construct(){

self::__construct()//对于父

//maybe create service for that?
    $reader = new Ini();
    $data = $reader->fromFile('config/config/config.ini');
    $this->layout()->setVariable("Data",$data);
}

布局中://代码

$data = $this->Data;