Symfony 2.6.4:命名空间问题?在命令行上使用自定义验证器传递抛出异常但在浏览器中工作正常

时间:2015-02-05 11:35:18

标签: php validation symfony symfony-2.6 php-5.6

我在this问题上尝试了Kevin Bond的解决方案。它在浏览器中使用应用程序时工作正常但在控制台命令上抛​​出以下异常。我为拼写错误检查了我的语法... 代码与上面链接的问题完全相同。我唯一改变的是捆绑名称。

$ php app/console 
<?
// src/AppBundle/DependencyInjection/Compiler/ValidatorPass.php
namespace AppBundle\DependencyInjection\Compiler;

use Symfony\Component\Finder\Finder;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class ValidatorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $validatorBuilder = $container->getDefinition('validator.builder');
        $validatorFiles = array();
        $finder = new Finder();

        foreach ($finder->files()->in(__DIR__ . '/../../Resources/config /validation') as $file) {
            $validatorFiles[] = $file->getRealPath();
        }

        $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));
    }
}


[RuntimeException]                                     
The autoloader expected class "AppBundle\DependencyInjection\Compiler\ValidatorPass"
to be defined in file "/home/mt/devel/netsite/phpprojekte/circle8/events/src/AppBundle/DependencyInjection/Compiler/ValidatorPass.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.                                                                                                                                                     

异常跟踪:

() at /.../vendor/symfony/symfony/src/Symfony/Component/Debug/DebugClassLoader.php:186
Symfony\Component\Debug\DebugClassLoader->loadClass() at n/a:n/a
spl_autoload_call() at /.../src/AppBundle/AppBundle.php:17
AppBundle\AppBundle->build() at /.../app/bootstrap.php.cache:2632
Symfony\Component\HttpKernel\Kernel->prepareContainer() at /.../app/bootstrap.php.cache:2611
Symfony\Component\HttpKernel\Kernel->buildContainer() at /.../app/bootstrap.php.cache:2564
Symfony\Component\HttpKernel\Kernel->initializeContainer() at /home/.../app/bootstrap.php.cache:2344
Symfony\Component\HttpKernel\Kernel->boot() at /.../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:70
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /.../vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
Symfony\Component\Console\Application->run() at /.../app/console:27

我尝试过任何可以想象的调试方式。请帮忙。 我现在唯一可以做的就是在使用控制台时在我的AppBundle.php中注释掉这个调用,并在使用浏览器时将其评论回来。

  • 用户和文件权限似乎无关紧要。
  • 清空缓存无济于事。

到目前为止尝试的事情:

  • 修复类

    的权限

    $ sudo chmod -R 777 src/AppBundle/DependencyInjection/ $ sudo -u daemon php app/console cache:clear --env=dev =&GT;同样的错误。

  • 删除缓存&amp;尝试热身

    $ sudo rm -rf app/cache/* $ sudo chmod 777 app/cache $ sudo app/console cache:warmup =&GT;同样的错误。

2 个答案:

答案 0 :(得分:0)

手动删除缓存(rm -rf样式),然后以root身份进行预热。修复权限,你就是GTG。

答案 1 :(得分:0)

我真的很沮丧,不知道我做错了什么。我修复了 真的 脏的方式......

我的AppBundle.php现在看起来像这样:

<?php

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use Symfony\Component\Finder\Finder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

//use AppBundle\DependencyInjection\Compiler\ValidatorPass;

class AppBundle extends Bundle
{
    // ...

    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new ValidatorPass());
    }

    // ...
}

class ValidatorPass implements CompilerPassInterface {

    public function process(ContainerBuilder $container)
    {
        $validatorBuilder = $container->getDefinition('validator.builder');
        $validatorFiles = array();
        $finder = new Finder();

        foreach ($finder->files()->in(__DIR__ . '/Resources/config/validation') as $file) {
            $validatorFiles[] = $file->getRealPath();
        }

        $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));
    }
}

我真的不喜欢这样,并且非常感谢能够真正解决问题。