Recaptcha2' non-object' Zend中的错误

时间:2018-04-13 20:14:23

标签: zend-framework recaptcha

我试图在使用Zend的旧项目中实现Recaptcha2。我正在使用此库https://packagist.org/packages/cgsmith/zf1-recaptcha-2

最初我收到此错误

Uncaught Zend_Exception: No entry is registered for key 'application'

在我的_initRecaptcha中,我更改了第一行

$config = \Zend_Registry::get('application');

$config = \Zend_Registry::get('config')->application;

这是我的_initRecaptcha现在

public function _initRecaptcha()
{
    $config = \Zend_Registry::get('config')->application;
    $params = $config->recaptcha->toArray();

    $params['messageTemplates'] = [
        \Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
        \Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
    ];

    \Zend_Registry::set('recaptcha', $params);
}

但现在我收到了这个错误

Trying to get property of non-object in /var/www/my-site/application/Bootstrap.php on line 79

第79行是_initRecaptcha

中的第一行

任何人都需要改变吗?

感谢。

1 个答案:

答案 0 :(得分:1)

图书馆似乎假设您的配置存储在密钥“应用程序”下的注册表中。 (这对我来说似乎有问题)。您可以在引导程序中添加一个方法来设置它(参见他们的示例:https://github.com/cgsmith/zf1-recaptcha-2/blob/master/example/application/Bootstrap.php#L13-L18),或者将代码更改为不需要它:

public function _initRecaptcha()
{
    $params = $this->getOptions()['recaptcha'];

    $params['messageTemplates'] = [
        \Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
        \Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
    ];

    \Zend_Registry::set('recaptcha', $params);
}

(自从我使用ZF1以来已经很久了,但我认为这应该有用。)

这两种情况都假设您已经根据图书馆教员向您的application.ini添加了recaptcha行。