How could read application.ini on controller using

2020-03-06 06:49发布

I have these lines in my application.ini

how can I read user in my contrroler

resources.doctrine.dbal.connections.default.parameters.driver   = "pdo_mysql"
resources.doctrine.dbal.connections.default.parameters.dbname   = "zc"
resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.port = 3306
resources.doctrine.dbal.connections.default.parameters.user = "root"
resources.doctrine.dbal.connections.default.parameters.password = "123456"

I use of this code but it retuens null

$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
 $user = $bootstrap->getOption('user');
var_dump($user);

edit: how may I read all of connections options ?

6条回答
神经病院院长
2楼-- · 2020-03-06 07:22

I think you should use

$this->getInvokeArgs('bootstrap');

For more info see this chapter in manual.

What about using

$conf = $bootstrap->getOption('resources');
$dbConf = $conf['doctrine']['dbal']['connections']['default']['parameters'];
查看更多
爷、活的狠高调
3楼-- · 2020-03-06 07:24

This goes in your controller.

$bootstrap = $this->getInvokeArg('bootstrap');
$appinidata = $bootstrap->getOptions();
$user=$appinidata['resources']['doctrine']['dbal']['connections']['default']['parameters'] ['user'];

This should print "root".

print_r($user);
查看更多
混吃等死
4楼-- · 2020-03-06 07:26

How about something like:

$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$connectionParams = $config->resources->doctrine->connections;

Or during Bootstrap, create and save this $config object in the Bootstrap or in the Zend_Registry for later retrieval in your controller.

查看更多
三岁会撩人
5楼-- · 2020-03-06 07:27

To get to the Doctrine container resource, just use :

$bootstrap = $this->getInvokeArg('bootstrap');
$doctrine = $bootstrap->getResource('doctrine');

From there you can drill down to the user name of the default connection (you can specify the connection if needed, just pass the name of the connection in the call to getConnection) :

$username = $doctrine->getConnection()->getUsername();
查看更多
手持菜刀,她持情操
6楼-- · 2020-03-06 07:33

In this case you should use Zend_Config_Ini class

$config = new Zend_Config_Ini('/path/to/config.ini','staging',$options);

second parameter is a section in INI file should be loaded ; third parameter is the key to allow modify loaded file.

You can put out value user this way:

$config->resources->doctrine->dbal->connections->default->parameters->user;
查看更多
祖国的老花朵
7楼-- · 2020-03-06 07:45

you can set any variable using set method as following on index.php inside the public folder Let

$config = 'test'; Zend_Registry::set('config', $config);

once the variable has been set then you can get on any controllers/models by following method

Zend_Registry::get('config');

Hop it Helps!!

查看更多
登录 后发表回答