2013-05-29 71 views
0

我有一個小問題在ZF2Zend框架2:看不見驗證碼

這裏生成的驗證碼是我的控制器:

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> 
... 

Abo ve代碼在data/captcha文件夾中生成png圖像,但我無法在視圖中生成它們。 FireBug顯示img元素和網址,但圖像網址似乎是空的。 感謝您的幫助。

+0

你./data/captcha目錄是從瀏覽器的網址 – kta

回答

1

你必須正確設置圖像網址,並在您module.config.php定義路由

控制器

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

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

記得測試/驗證碼更改爲自己的基礎URL

in your registerForm class

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

將此添加到您的module.confi g.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; 
} 

這些,應該是工作的罰款

+0

如果你想好黑客無法訪問將圖像放到公共目錄之外。 :) – kta