• Zend Framework 配置文件怎么写?


    Zend Framework 配置文件怎么写呢?

    Zend Framework 版本 1.10

    在应用程序的初始化引导过程中,主要由Zend_Application类对配置文件进行加载。Zend_Application_Bootstrap_BootstrapAbstract类也会根据配置文件加载相应的引导资源类。因此我们只要看看Zend_Application类的setOptions()方法和Zend_Application_Bootstrap_BootstrapAbstract的setOptions()方法,便大概可以知道配置文件应该怎么写了。

    下面以Ini配置文件为例。

    先看Zend_Application类的setOptions()方法

    /**
    * Set application options
    *
    * @param array $options
    * @throws Zend_Application_Exception When no bootstrap path is provided
    * @throws Zend_Application_Exception When invalid bootstrap information are provided
    * @return Zend_Application
    */
    public function setOptions(array $options)
    {
    //如果键config不为空,加载config文件
    if (!empty($options['config'])) {
    if (is_array($options['config'])) {
    $_options = array();
    foreach ($options['config'] as $tmp) {
    $_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));
    }
    $options = $this->mergeOptions($_options, $options);
    }
    else {
    $options = $this->mergeOptions($this->_loadConfig($options['config']), $options);
    }
    }

    $this->_options = $options;

    $options = array_change_key_case($options, CASE_LOWER);

    $this->_optionKeys = array_keys($options);

    //设置php的配置
    if (!empty($options['phpsettings'])) {
    $this->setPhpSettings($options['phpsettings']);
    }

    //设置加载路径
    if (!empty($options['includepaths'])) {
    $this->setIncludePaths($options['includepaths']);
    }

    //设置自动加载的命名空间
    if (!empty($options['autoloadernamespaces'])) {
    $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
    }

    //设置自动加载zend framework 的路径
    if (!empty($options['autoloaderzfpath'])) {
    $autoloader = $this->getAutoloader();
    if (method_exists($autoloader, 'setZfPath')) {
    $zfPath = $options['autoloaderzfpath'];
    $zfVersion = !empty($options['autoloaderzfversion'])
    ? $options['autoloaderzfversion']
    : 'latest';
    $autoloader->setZfPath($zfPath, $zfVersion);
    }
    }

    //设置自定义的bootstrap类
    if (!empty($options['bootstrap'])) {
    $bootstrap = $options['bootstrap'];

    if (is_string($bootstrap)) {
    $this->setBootstrap($bootstrap);
    }
    elseif (is_array($bootstrap)) {
    if (empty($bootstrap['path'])) {
    throw new Zend_Application_Exception('No bootstrap path provided');
    }

    $path = $bootstrap['path'];
    $class = null;

    if (!empty($bootstrap['class'])) {
    $class = $bootstrap['class'];
    }

    $this->setBootstrap($path, $class);
    }
    else {
    throw new Zend_Application_Exception('Invalid bootstrap information provided');
    }
    }

    return $this;
    }

    首先,如果"config"不为空,递归加载配置文件。

    这也就是说:我们可以有多个配置文件,但是在初始化Zend_Application对象的时候,只要把主配置文件的路径作为参数传给Zend_Application的构造函数就可以了。我们可以把不同的配置分成多个配置文件,如php配置的写在一个配置文件,数据库的配置写一个配置文件,引导资源的配置写一个配置文件,然后写一个主配置文件,其它包含之前的那些配置文件就可以了,如:

    主配置文件:

    [yourenvironment]
    config.phpsettingconfig
    = APP_ROOT "/config/phpsettingconfig.ini"
    config.dbconfig
    = APP_ROOT "/config/dbconfig.ini"
    config.resourceconfig
    = APP_ROOT "/config/resourceconfig.ini"

    这样,我们就不必把配置都写在一个配置文件中,可以分开几个,逻辑划分上更加清晰。

    然后,键为phpsettings的,调用setPhpSettings()方法进行设置

    也就是说php配置我们也可以写在Zend Framework的配置文件里。如下面是phpsettingconfig.ini的内容

    
    
    [yourenvironment]
    phpSettings.date.timezone = "Asia/Shanghai"
    phpSettings.display_startup_errors
    = 1
    phpSettings.display_errors
    = 1

    此设置跟php配置文件php.ini基本一样,只要在设置项前加phpSettings就可以了(phpSettings大小写不限)

    再看一下如果在配置文件里设置自定义的bootstrap文件

    [yourenvironment]
    #可以是一个文件名字符串,此时你的自定义bootstrap类的类名必须是默认的
    "Bootstrap"
    bootstrap
    = APP_ROOT "/application/bootstrap.php"
     

    #如果你的类名不叫
    "Bootstrap"而是叫"MyBootstrap",你可以这样配置
    bootstrap.path
    = APP_ROOT "/applicaton/bootstrap.php"
    bootstrap.class
    = "MyBootstrap"

     在Zend_Application中对配置项的引导,大概就这些。

    引导资源的初始化,主要是在Zend_Application_Bootstrap_BootstrapAbstract类的setOptions()方法中。

    Zend_Application_Bootstrap_BootstrapAbstract类的setOptions()方法主要功能是根据配置文件设置引导资源的加载目录,以及根据配置文件初始化引导资源。根据配置文件初始化引导资源主要体现在如下代码中:

    if (in_array($method, $methods)) {
    $this->$method($value);
    }
    elseif ('resources' == $key) {
    //或者是注册引导资源
    foreach ($value as $resource => $resourceOptions) {
    $this->registerPluginResource($resource, $resourceOptions);
    }
    }

    也就是说配置文件中resources开头的配置项,都会作为引导资源

    如数据库的配置

    [yourenvionment]
    resources.db.adapter
    = PDO_MYSQL
    resources.db.params.host
    = localhost
    resources.db.params.username
    = root
    resources.db.params.password
    =
    resources.db.params.dbname
    = test

    如果把ini文件描述成一个数组,那么$ini['resources']['db']将作为一个构造函数的参数,传给Zend_Application_Resource_ResourceAbstract的构造函数。

    然后,adapter 映射 Zend_Application_Resource_Db类的中setAdapter()方法,$ini['resources']['db']['adapter']的值作为setAdapter()方法的参数。params映射Zend_Application_Resource_Db类的中setParams()方法,$ini['resources']['db']['params']的值作为setParams()方法的参数。

  • 相关阅读:
    [oracle 使用(1)] win7/10 下Oracle10g的安装与卸载
    [原创]利用爬虫技术获取网页数据,以及想要的指定数据
    [mysql使用(1)] 64位Linux下安装mysql-5.7.13-linux-glibc2.5-x86_64
    aop 常见的问题
    Git技巧:右键菜单怎么去除?
    jrebel激活
    python与mysql连接
    linux下mysql的安装
    Linux下jdk和tomcat安装
    Lotto
  • 原文地址:https://www.cnblogs.com/rexy/p/1735264.html
Copyright © 2020-2023  润新知