添加 FOSUserBundle 后出现“无法找到实体”异常

exception `Unable to find entity` after the addition of FOSUserBundle

我按照书中的步骤进行操作(基于 symfony 2.0.10,我正在使用 Symfony2.6.1 和 FOSUB 2.0)。

我用 CRUD 构建项目,可以很好地显示来自数据库的数据(My/BackendBundle 内的 Mountain 控制器)。

然后我只想为登录用户管理员显示项目(ROLE_SUPER_ADMIN,存在于我的 for_user table 内部数据库中,带有 CRUD 数据)。

但是打开../web/ 时结果异常Unable to find Mountain entity。来自 Mountain 控制器 showAction($id)

#\src\My\BackendBundle\Controller\MountainController.php
/**
 * Finds and displays a Mountain entity.
 *
 * @Route("/{id}", name="mountain_show")
 * @Method("GET")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('MyBackendBundle:Mountain')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Mountain entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );

AppKernel.php

#\app\AppKernel.php
    new FOS\UserBundle\FOSUserBundle(),
    new My\UserBundle\MyUserBundle(),
    new My\BackendBundle\MyBackendBundle(),

security.yml

#\app\config\security.yml
security:
    providers:
        fos_userbundle:
            id: fos_user.user_manager

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        main:
            pattern: ^/
            logout:       true
            anonymous:    true
            form_login:
                provider:                       fos_userbundle
                csrf_provider:                  form.csrf_provider
                login_path:                     /login
                use_forward:                    false
                check_path:                     /login_check
                post_only:                      true
                always_use_default_target_path: false
                default_target_path:            /
                target_path_parameter:          _target_path
                use_referer:                    false
                failure_path:                   null
                failure_forward:                false
                username_parameter:             _username
                password_parameter:             _password
                csrf_parameter:                 _csrf_token
                intention:                      authenticate

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, role: ROLE_SUPER_ADMIN }

config.yml

#\app\config\config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

framework:
    #esi:             ~
    translator:      ~
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # default_locale: pl
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }


stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            tree: false
            loggable: false
            timestampable: false
            sluggable: false
            translatable: false

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: My\UserBundle\Entity\User

routing.yml

#\app\config\routing.yml
MyBackendBundle:
    resource: "@MyBackendBundle/Controller/"
    type:     annotation
    prefix:   /

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

parameters.yml

#\parameters.yml
parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_name: koronaziemi
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    locale: pl
    secret: ThisTokenIsNotSoSecretChangeIt

requirements 参数添加到您的路线描述中,您也可以通过将其留给 Symfony 为您服务来跳过实体获取:

#\src\My\BackendBundle\Controller\MountainController.php
/**
 * Finds and displays a Mountain entity.
 *
 * @Route("/{mountain}", name="mountain_show", requirements={"mountain": "\d+"})
 * @Method("GET")
 * @Template()
 */
public function showAction(Mountain $mountain)
{
    $deleteForm = $this->createDeleteForm(mountain->getId());

    return array(
        'entity'      => $mountain,
        'delete_form' => $deleteForm->createView(),
    );
}

此外,@Template() 被认为是不好的做法,return 一个 Response 对象是一个更好的主意:

#\src\My\BackendBundle\Controller\MountainController.php
/**
 * Finds and displays a Mountain entity.
 *
 * @Route("/{mountain}", name="mountain_show", requirements={"mountain": "\d+"})
 * @Method("GET")
 */
public function showAction(Mountain $mountain)
{
    $deleteForm = $this->createDeleteForm(mountain->getId());

    return $this->render(
        'MyBackendBundle:Mountain:show',
        array(
            'entity'      => $mountain,
            'delete_form' => $deleteForm->createView(),
        )
    );
}

----编辑---

@Aistis 使用此代码

 /**
 * @Route("/{id}", name="mountain_show", requirements={"id": "\d+"}))
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();
    var_dump($id);
    $entity = $em->getRepository('MyBackendBundle:Mountain')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Mountain entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );
}

它的作品。但是为什么他得到/login作为参数呢?