Zend Framework 2でキャプチャをカスタマイズする

Zend \ Captchaコンポーネントは、論理的な質問をする、歪んだフォントを生成する、複数の画像を送信する、それらの間の接続を確立するなど、さまざまな形式をとることができます。 Zend \ Captchaは、オフラインまたはZend \ Formコンポーネントと組み合わせて使用​​できるさまざまなサーバーソリューションを提供することを目的としています。



Captcha要素には、次々にレンダリングされる複数のフィールドがあります。 組み込みのcaptcha画像ジェネレーター( Zend \ Captcha \ Image.php )は、独自のヘルパー( Zend \ Form \ View \ Helper \ Captcha \ Image.php )を使用して画像を作成します。 また、 Zend \ Captcha \ Image.phpには、メソッド 'getHelperName'があります。 このメソッドは、キャプチャ画像をレンダリングするためのヘルパー名を渡します。 デフォルトでは、「getHelperName」は「captcha / image」-Zend \ Form \ View \ Helper \ Captcha \ Image.phpクラスのインスタンスを渡します。 デバッガーを使用してさらに深く掘り下げると、インスタンスプロパティphpRenderer :: __ helpersで、 キャプターヘルパーがinvokablesClassesにあることがわかります。 これは、前述のZend \ Form \ View \ Helper \ Captcha \ Image.phpです。 大まかに言うと、画像を作成するだけで、レンダラーはヘルパーを使用して残りの作業を行いますが、この状況は全員に適しているわけではありません。



Captchaフォーム要素( Zend \ Form \ Element \ Captcha.php )を作成するとき、captchaイメージ( Zend \ Captcha \ Image.php )をこの要素に渡します。 Captchaフォーム要素には、独自のヘルパー( Zend \ Form \ View \ Helper \ FormCaptcha.php )もあります。 このヘルパーのrenderメソッドでは、captchaイメージ( Zend \ Captcha \ Image.php )がElementInterfaceを使用してロードされていることがわかります。



//Zend\Form\View\Helper\FormCaptcha.php public function render(ElementInterface $element) { $captcha = $element->getCaptcha();
      
      







その後、「getHelperName」メソッドを介してヘルパーが呼び出されます。



 $helper = $captcha->getHelperName();
      
      







その結果、PhpRendererのインスタンスを介してヘルパークラスのインスタンスを取得し、ビューを返します。



 $helper = $renderer->plugin($helper); return $helper($element);
      
      







「Captchaフォーム要素とそのヘルパー」と「Captchaのイメージとそのヘルパー」を明確に区別する必要があります。 「キャプチャイメージ」をフォームの「Captcha」要素に渡します。この要素には、ヘルパーが既にアタッチされています。 「Captchaフォーム要素」のrenderメソッドは、ヘルパーを使用して渡された「Captcha画像」のインスタンスを埋め込み、画像を生成してビューを返します。 必要なのは、新しいヘルパーを 'Captcha image'に渡すことです。これにより、ビューが好きな方法で表示され、 Zend \ Captcha \ Image.phpが書き換えられ、デフォルトのヘルパーではなく、新しいヘルパーが受信されます。



始める前に、いくつかの詳細を見てみましょう。

Captchaを表示するZend \ Form \ View \ Helper \ Captcha \ Image.phpヘルパーは、パターンを%s%s%sとして定義します。



 $pattern = '%s%s%s';
      
      







したがって、最初に必要なのは、独自のパターンを持つカスタムヘルパーです。 Applicationモジュールで作成しましょう:



 //module\Application\src\Application\View\Helper\Form\Captcha\ViewHelperCaptcha.php <?php namespace Application\View\Helper\Form\Captcha; use Zend\Form\View\Helper\Captcha\AbstractWord; use Application\View\Helper\Form\Captcha\CustomCaptcha as CaptchaAdapter; use Zend\Form\ElementInterface; use Zend\Form\Exception; class ViewHelperCaptcha extends AbstractWord { /** * Override * * Render the captcha * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { //         . $this->setSeparator('') $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf( '%s requires that the element has a "captcha" attribute of type Zend\Captcha\Image; none found', __METHOD__ )); } //     (  600). $captcha->setExpiration(10); //    (  10).        . $captcha->setGcFreq(1); $captcha->generate(); $imgAttributes = array( 'width' => $captcha->getWidth(), 'height' => $captcha->getHeight(), 'alt' => $captcha->getImgAlt(), 'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(), ); $closingBracket = $this->getInlineClosingBracket(); $img = sprintf( '<img %s%s', $this->createAttributesString($imgAttributes), $closingBracket ); $position = $this->getCaptchaPosition(); $separator = $this->getSeparator(); $captchaInput = $this->renderCaptchaInputs($element); //   $pattern = '<div class="captcha_image"> %s</div> %s<div class="captcha_input"> %s</div>' if ($position == self::CAPTCHA_PREPEND) { return sprintf($pattern, $captchaInput, $separator, $img); } return sprintf($pattern, $img, $separator, $captchaInput); } }
      
      







ヘルパークラスは、 Zend \ Form \ View \ Helper \ Captcha \ Image.phpクラスを少し変更して複製します。 ヘルパーは、オリジナルとは異なり、 Zend \ Captcha \ Image.phpを使用して画像を生成しません。 Zend \ Captcha \ Image.phpはメソッド 'getHelperName'を提供し、ハードコードされたヘルパー名 'captcha / image'を返すので、フォームが画像を生成するとき、独自のデフォルトヘルパーを受け取らず、作成されます私たちによって。 他に必要なのは、カスタムヘルパーをphpRendererに渡し、元のZend \ Captcha \ Image.phpクラスを拡張する新しいcaptchaイメージを生成し、作成したヘルパーの名前を設定して 'getHelperName'メソッドを書き換えることです。



したがって、クラスをphpRenderer invokablesヘルパー構成に追加しましょう。 これをmodule.config.phpに実装します。



 //module\Application\config\module.config.php ... 'view_helpers' => array( 'invokables' => array( 'viewhelpercaptcha' => 'Application\View\Helper\Form\Captcha\ViewHelperCaptcha', ), ),
      
      







次のステップは、phpRenderer invokablesクラスとしてモジュール構成に追加されたヘルパーを返すcaptchaイメージを作成することです。 クラス全体のZend \ Captcha \ Image.phpを再定義する必要はありません。カスタムヘルパークラスのメソッド 'getHelperName'をパラメーターとして指定するだけです。 これを行うには、クラスを作成し、フォルダモジュール\ Application \ src \ Application \ View \ Helper \ Form \ CaptchaCustomCaptcha.phpなどの名前を付けます。 元のZend \ Captcha \ Image.phpクラスを拡張し、「gethelperName」メソッドをオーバーライドして、「viewhelpercaptcha」ヘルパーを返すようにします。 また、$ messageTemplatesプロパティのエラーメッセージをオーバーライドするのにも役立ちます。 あなたの裁量で。



 //module\Application\src\Application\View\Helper\Form\Captcha\CustomCaptcha.php <?php namespace Application\View\Helper\Form\Captcha; // ,   . use Zend\Captcha\Image as CaptchaImage; //  ,      . class CustomCaptcha extends CaptchaImage { protected $messageTemplates = array( self::MISSING_VALUE => ' ', self::MISSING_ID => ' ID ', self::BAD_CAPTCHA => '  ', public function getHelperName() { return 'viewhelpercaptcha'; } }
      
      







最後に行う必要があるのは、フォームでCustomCaptchaイメージを使用することです。 これを行うには、2つのフォルダーを作成します。1つはcaptchaワードを生成するために必要なフォント(zf2folder / data / fonts)用で、もう1つはcaptcha画像ファイル(zf2folder / public / img / captcha)を保存します。 当然、fontsフォルダーで、* .ttfフォント、たとえばarial.ttfをコピーします。



たとえば、公式チュートリアルのフォームを使用できます。



 <?php namespace Album\Form; use Zend\Form\Form; use Application\Form\View\Helper\Captcha\CustomCaptcha; class AlbumForm extends Form { public function __construct($name = null) { //    parent::__construct('album'); $this->setAttribute('method', 'post'); //    ... //    Captcha $dirdata = './data'; //  CustomCaptcha  $captchaImage = new CustomCaptcha(array( 'font' => $dirdata . '/fonts/arial.ttf', 'width' => 120, 'height' => 60, 'fsize' => 20, 'wordLen' => 5, 'dotNoiseLevel' => 25, 'lineNoiseLevel' => 2 )); //     $captchaImage->setImgDir('public/img/captcha/'); //      $captchaImage->setImgUrl('/img/captcha/'); $captchaImage->setImgAlt('   ?'); //   Captcha    CustomCaptcha,   $this->add(array( 'type' => 'Zend\Form\Element\Captcha', 'name' => 'captcha', 'options' => array( 'captcha' => $captchaImage, ), 'attributes' => array( 'class' => 'some_class', ) )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'submit', 'value' => 'Go', 'id' => 'submitbutton', ), )); } }
      
      







ビューファイルにキャプチャの要素を追加します



 echo $this->formRow($form->get('captcha')) . PHP_EOL;
      
      







出力されるHTMLドキュメントの構造を調べた後、画像とcaptcha入力フィールドがdivでラップされていることがわかります。 より合理化された設計ができました。



使用されるソース:

framework.zend.com/manual/2.2/en/modules/zend.captcha.intro.html

framework.zend.com/manual/2.2/en/modules/zend.captcha.operation.html

framework.zend.com/manual/2.2/en/modules/zend.captcha.adapters.html

framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html

zendtemple.blogspot.com/2012/12/zend-framework-2-zf2-creating-view.html

samsonasik.wordpress.com/2012/09/12/zend-framework-2-using-captcha-image-in-zend-form




All Articles