加载资源security.yml symfony3

时间:2017-05-04 10:33:31

标签: php symfony security symfony-3.2

我尝试使用我自己的bundle中的security.yml在app中扩展我的文件security.yml,但是我发现了这个错误:

InvalidArgumentException in YamlFileLoader.php line 431:
There is no extension able to load the configuration for "security"(in
C:\wamp64\www\Project\src\Admin\UserBundle\DependencyInjection/../Resources/
config\security.yml). Looked for namespace "security", found none

我看过Symfony的文档,但我找不到问题。我告诉你我的代码

应用程序/资源/配置/ security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
    providers:
        in_memory:
            memory: ~
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

管理员/ UserBundle /资源/配置/ security.yml

security:
    encoders:
        Admin\UserBundle\Entity\User: sha512
    role_hierarchy:
        ROLE_COLLABORATEUR:       [ROLE_LECTEUR]
        ROLE_CHEF:       [ROLE_COLLABORATEUR]
        ROLE_ADMIN:       [ROLE_CHEF, ROLE_ALLOWED_TO_SWITCH]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    providers:
        main:
            id: fos_user.user_provider.username
        firewalls:
            main_login:
                pattern:   ^/login$
                anonymous: true
            main_register:
                pattern:   ^/register
                anonymous: true
            main_resetting:
                pattern:   ^/resetting
                anonymous: true
            main:
                anonymous: false
                pattern: ^/
                provider:  main
                form_login:
                    login_path: fos_user_security_login
                    check_path: fos_user_security_check
                logout:
                    path:       fos_user_security_logout
                target:     ^/
                remember_me:
                    secret: %secret%

管理员/ UserBundle / DependencyInjection / AdminUserExtension.php

class AdminUserExtension extends Extension
{
    /**
    * {@inheritdoc}
    */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

        foreach ( array('services', 'parameters', 'security') as $basename )
        {
           $loader->load(sprintf('%s.yml', $basename));
        }
    }
}

SecurityBundle在这里:(

AppKernel

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new FOS\UserBundle\FOSUserBundle(),
            new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function getRootDir()
    {
        return __DIR__;
    }

    public function getCacheDir()
    {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
    }

    public function getLogDir()
    {
        return dirname(__DIR__).'/var/logs';
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
 }

另一个文件似乎有效,因为当我在它们之间合并security.yml时,应用程序可以工作。

1 个答案:

答案 0 :(得分:0)

您需要将SecurityBundle添加到AppKernel。

    public function registerBundles()
{
    $bundles = [
        ...
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        ...
    ];