简体   繁体   中英

Zend framework 2 : How do I access modules config value from a controller

In module.config.php file, I have set value for 'password_has_type'. And in controller I want to access that. Here is my module.config.php file:

'auth' => array(
    'password_hash_type' => 'sha512',
),
'di' => array(
    'instance' => array(
        'alias' => array(
            'auth' => 'Auth\Controller\AuthController',
            'auth_login_form' => 'Auth\Form\LoginForm',
        ),...

In controller , I have used

use Auth\Module

and in Action method I try to get access value by

echo Module::getOption('password_hash_type');

But I could not get any value?

So please can anybody help me to get that value ?

Please see my answer at Access to module config in Zend Framework 2 .

But to make it more concrete to your question, you would do this:

$config = $this->getServiceLocator()->get('Config');
$pwht = $config['auth']['password_hash_type'];

I hope this helps!

You can do it with help of aliases and parameters. Put it into di->instance array:

'Auth\Controller\AuthController' => array(
    'parameters' => array(
        'passwordHashType' => 'sha512'
    )
),

And it is your controller:

namespace Auth\Controller;
use Zend\Mvc\Controller\ActionController;

class AuthController extends ActionController
{
    protected $passwordHashType;

    public function indexAction()
    {
        echo $this->passwordHashType;
    }

    public function setPasswordHashType($passwordHashType)
    {
        $this->passwordHashType = $passwordHashType;
        return $this;
    }
}

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