在 Symfony2 (2.8) 中使用验证码

Using Captcha with Symfony2 (2.8)

我使用以下 Instruction:

安装了验证码包
  1. "gregwar/captcha-bundle": "1.0.0" 添加到 composer.json
  2. 中的 require 部分
  3. 运行 Windows PowerShell 在 root 中调用 php composer.phar update
  4. 控制台输出

Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_yaml.dll' - Nie mo┐na odnalečŠ okreťlonego modu│u. in Unknown on line 0 Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Generating autoload files Incenteev\ParameterHandler\ScriptHandler::buildParameters Updating the "app/config/parameters.yml" file Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap

Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_yaml.dll' - Nie mo┐na odnalečŠ okreťlonego modu│u. in Unknown on line 0 Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_yaml.dll' - Nie mo┐na odnalečŠ okreťlonego modu│u. in Unknown on line 0

// Clearing the cache for the dev environment with debug true

[OK] Cache for the "dev" environment (debug=true) was successfully cleared.

Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets

Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_yaml.dll' - Nie mo┐na odnalečŠ okreťlonego modu│u. in Unknown on line 0

Trying to install assets as relative symbolic links.

        Bundle                 Method / Error   

WARNING FrameworkBundle copy
WARNING JMSTranslationBundle copy

! [NOTE] Some assets were installed via copy. If you make changes to these assets you have to run this command again.

[OK] All assets were successfully installed.

Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

  1. 按照说明我可以跳过这一步

    // app/autoload.php
    
    $loader->registerNamspaces(array(
        // ...
        'Gregwar' => __DIR__.'/../vendor/bundles',
    ));
    

但我的 autoload.php 文件如下所示:

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

error_reporting(error_reporting() & ~E_USER_DEPRECATED);

$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;
  1. 我启用了捆绑包:

    // app/appKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Gregwar\CaptchaBundle\GregwarCaptchaBundle(),
        );
    }
    
  2. 最后的安装步骤是将 gregwar_captcha: ~ 添加到 app/config/config.yml 并完成。

现在我正尝试在我的控制器中使用它。

    public function registrationAction(Request $request)
    {
        $user = new Models\User();

        $form = $this->createFormBuilder($user)
            ->add('username', 'Symfony\Component\Form\Extension\Core\Type\TextType')
            ->add('birth', 'Symfony\Component\Form\Extension\Core\Type\DateType')
            ->add('captcha', 'captcha')
            ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType', array('label' => 'Register'))
            ->getForm();

        $form->handleRequest($request);



        return $this->render(
             'CassyW2Bundle:User:registration.html.twig',
             array(
                'form' => $form->createView(),
            )
        );
    }

我收到错误:

Compile Error: Declaration of Gregwar\CaptchaBundle\Type\CaptchaType::buildView() must be compatible with Symfony\Component\Form\FormTypeInterface::buildView(Symfony\Component\Form\FormView $view, Symfony\Component\Form\FormInterface $form, array $options)

我哪里错了?

参见Doc。对于你的 symfony 版本,你需要这个包的另一个版本。尝试安装它而不提供 composer.json.

中的版本

根据警告,尝试通过从 PEAR 的网站 https://pecl.php.net/package/yaml.

下载来安装 php_yaml 扩展

选择稳定版,解压后复制dll到**C:\xampp\php\ext**

https://github.com/symfony/symfony/blob/2.7/UPGRADE-2.1.md#form

如果你查看升级文档,其中与 Symfony 2.1 FormTypeInterface 相关的版本发生了变化,它是一个 BC 中断,用于 https://github.com/Gregwar/CaptchaBundle/blob/v1.0.0/Type/CaptchaType.php

所以 Symfony 2.8 与这个包版本 1.0.0 不兼容有版本 2.0 标记,请使用这个,如果还有问题打开另一个问题。