简体   繁体   中英

in Zend instead of application.ini, can i have my configuration store in php file like config.php

I am working on a project based on Zend 1.12. There is a new requirement in which we have to encode all of our files to prevent hacking. We used ioncube encoder which encode all of the .php file but not application.ini where it store db information username, password, secret key etc. There are two approach :

1) Have configuration reside in .php file such as config.php instead of application.ini

2) have ioncube encode application.ini

With the first approach I found Zend Config Introduction where you could have configuration store in array. I need a concrete example with setup in Bootstrap.php where it could utilize these configuration. Right now all of our model extends from Zend_Db_Table_Abstract. I really want when i migrate all the application.ini db configuration into config.php all the db operation works and there are several instance in front controller make use of $this->applicationOptions. I hope my putting configuration this will work as well. Please help

With second approach I did not found much resolution on ioncube be able to encode application.ini

This is my configuration file

return $configArray = array(

  'db' => array( 'adapter' => 'pdo_mysql', 'params' => array( 'host' => 'localhost', 'username' => 'root', 'password' => 'root', 'dbname' => 'test' ) ) ); 

When i do this in Bootstrap.php and it work

  $config = new Zend_Config(require APPLICATION_PATH .'/configs/config.php'); $db = Zend_Db::factory($config->db->adapter, $config->dv->params->toArray()); $db = Zend_Db::factory($config->db); Zend_Db_Table::setDefaultAdapter($db); 

but when i do

  protected function _initDatabase(){ $config = new Zend_Config(require APPLICATION_PATH .'/configs/config.php'); $resource = $this->getPluginResource('db'); var_dump($this->getPluginResource('db')); // this will be null. 

My question is is should i do anything different in configuration so it will mimic resources.db.adapter, resources.db.params.host etc and it will be picking up by the pluginResources or getoptions

Normally it is expected that users would use one of the adapter classes such as Zend_Config_Ini or Zend_Config_Xml, but if configuration data are available in a PHP array, one may simply pass the data to the Zend_Config constructor in order to utilize a simple object-oriented interface.

Basically, you first create a PHP file that contains the configuration :

// config.php
return array(
  ...
  ...
);

And, then, from another file, use than configuration file :

$config = new Zend_Config(require 'config.php');

In your Bootstrap.php try getting the configuration like this

protected function _initDb()
{
    $resource = $bootstrap->getPluginResource('db');

    $db = $resource->getDbAdapter();

    Zend_Registry::set("db", $db);
}

After registering the db variable in your Bootstrap.php you can access it like this

$dbAdapter = Zend_Registry::get("db");

You use getPluginResource() hence you need to have your configuration keys in the following way:

resources.db.adapter = ...
resources.db.params.host = ...
...

or your config.php should look like this:

// config.php
return array(
  'resources' => array(
    'db' => array(
       ...
    ),
  ),
  ...
);

This page could be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM