0

我在 ZF2 中生成验证码时遇到了一点问题

这是我的控制器:

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

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

注册表格类:

    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 似乎为空。谢谢你的帮助。

4

1 回答 1

1

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

控制器

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

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

请记住将测试/验证码更改为您自己的基本网址

在您的 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',
                                         ),
                                    ),
                                ),
                            ),

记得改captcha image下的controller,action generate其实就是生成captcha image文件

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

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;
}

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

于 2013-10-16T05:13:41.670 回答