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

萝らか妹 提交于 2019-12-11 09:08:15

问题


I follow steps from my book (based on symfony 2.0.10, I'm using Symfony2.6.1 with FOSUB 2.0).

I build project with CRUD that display data from db just fine (Mountain controller inside My/BackendBundle).

Then I want display project only for logged user admin (with ROLE_SUPER_ADMIN, that exist in my for_user table inside db with CRUD data).

But when openning ../web/ its result exception Unable to find Mountain entity. from Mountain controller 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


回答1:


Add requirements parameter to your route description and also you can skip entity fetching by leaving it for Symfony to serve you:

#\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(),
    );
}

Also, @Template() considered as bad practice and it is a better idea to return a Response object:

#\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(),
        )
    );
}



回答2:


----edit---

@Aistis with this code

 /**
 * @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(),
    );
}

it's works. But why he gets /login as a parameter?



来源:https://stackoverflow.com/questions/28088122/exception-unable-to-find-entity-after-the-addition-of-fosuserbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!