Zend Framework 2:Invisible Captcha

时间:2013-05-29 11:46:11

标签: php zend-framework zend-framework2

我在ZF2中生成验证码有点问题

这是我的控制器:

public function indexAction()
{
    $form = new RegisterForm();

    return array('form' => $form);
}

RegisterForm类:

    public function __construct($name = null)
    {
        $this->url = $name;
        parent::__construct('register');
        $this->setAttributes(array(
            'method' => 'post'
        ));

        $this->add(array(
            'name' => 'password',
            'attributes' => array(
                'type'  => 'password',
                'id' => 'password'
            )
        ));
        $this->get('password')->setLabel('Password: ');

        $captcha = new Captcha\Image();
        $captcha->setFont('./data/captcha/font/arial.ttf');
        $captcha->setImgDir('./data/captcha');
        $captcha->setImgUrl('./data/captcha');



        $this->add(array(
            'type' => 'Zend\Form\Element\Captcha',
            'name' => 'captcha',
            'options' => array(
                'label' => 'Captcha',
                'captcha' => $captcha,
            ),
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'button',
                'value' => 'Register',
            ),
        ));
}

查看:index.phtml:

...
<div class="group">
    <?php echo $this->formlabel($form->get('captcha')); ?>
    <div class="control-group">
        <?php echo $this->formElementErrors($form->get('captcha')) ?>
        <?php echo $this->formCaptcha($form->get('captcha')); ?>
    </div>
</div>
...

上面的代码在data / captcha文件夹中生成png图像,但我无法在视图中生成它们。 FireBug显示img元素和url,但是url to image似乎是空的。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您必须正确设置img url并在module.config.php中定义路由

控制器

public function indexAction()
{
    $form = new RegisterForm($this->getRequest()->getBaseUrl().'test/captcha');

    return array('form' => $form);
}

请记住将test / captcha更改为您自己的基本网址

在您的registerForm类

public function __construct($name)
{
    //everything remain the same, just change the below statement
    $captcha->setImgUrl($name);
}

将此添加到您的module.config.php,作为主路线子

                            'may_terminate' => true,
                            'child_routes' => array(
                                'registerCaptcha' => array(
                                    'type'    => 'segment',
                                    'options' => array(
                                        'route'    => '/[:action[/]]',
                                         'constraints' => array(
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                        ),
                                            'defaults' => array(
                                                'action' => 'register',                     
                                            ),
                                    ),
                                ),

                                'captchaImage' => array(
                                    'type'    => 'segment',
                                    'options' => array(
                                        'route'    =>  '/captcha/[:id]',
                                         'constraints' => array(
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                        ),
                                        'defaults' => array(
                                            'controller' => 'controller',
                                            'action'     => 'generate',
                                         ),
                                    ),
                                ),
                            ),

记得在验证码图像下更改控制器,动作生成实际上是生成验证码图像文件

将此添加到您的控制器文件

public function generateAction()
{
    $response = $this->getResponse();
    $response->getHeaders()->addHeaderLine('Content-Type', "image/png");

    $id = $this->params('id', false);

    if ($id) {

        $image = './data/captcha/' . $id;

        if (file_exists($image) !== false) {
            $imagegetcontent = @file_get_contents($image);

            $response->setStatusCode(200);
            $response->setContent($imagegetcontent);

            if (file_exists($image) == true) {
                unlink($image);
            }
        }

    }

    return $response;
}

有了这些,它应该可以正常工作