Zend框架2:如何从控制器访问模块配置值

Sai*_*man 8 php zend-framework

在module.config.php文件中,我为'password_has_type'设置了值.在控制器中我想访问它.这是我的module.config.php文件:

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

controller,我用过

use Auth\Module
Run Code Online (Sandbox Code Playgroud)

并在Action方法中我尝试获取访问值

echo Module::getOption('password_hash_type');
Run Code Online (Sandbox Code Playgroud)

但我无法获得任何价值?

那么请有人帮助我获得这个价值吗?

tom*_*omo 5

请参阅我在Zend Framework 2访问模块配置的答案.

但为了使你的问题更具体,你会这样做:

$config = $this->getServiceLocator()->get('Config');
$pwht = $config['auth']['password_hash_type'];
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!


Ale*_*tau 0

您可以借助别名和参数来完成此操作。将其放入di->instance数组中:

'Auth\Controller\AuthController' => array(
    'parameters' => array(
        'passwordHashType' => 'sha512'
    )
),
Run Code Online (Sandbox Code Playgroud)

这是你的控制器:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)