Symfony将选民注入表格类型

时间:2018-04-18 08:45:16

标签: php symfony

您好我试图在我的UserType中访问我的选民。我试图在这个主题How to customize form field based on user roles in Symfony2/3?

中遵循优雅的第二个答案(Bart Bartoman)

我收到了这个错误 Catchable Fatal Error: Argument 1 passed to PortalBundle\Form\UserType::__construct() must be an instance of Symfony\Component\Security\Core\Authorization\AuthorizationChecker, none given, called in /var/www/html/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 92

portal.security.form.user:
        class: PortalBundle\Form\FormType
        arguments: ['@security.authorization_checker']
        tags:
            - { name: form.type }

我的UserType.php的开头

use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;

class UserType extends AbstractType
{
    private $authorization;
    public function __construct(AuthorizationChecker $authorizationChecker)
    {
        $this->authorization = $authorizationChecker;
    }

这是我从控制器调用它的方式 $editForm = $this->createForm(UserType::class, $user);

我正在使用symfony 3.4

编辑:添加了我的security.yml

services:
    hwi_oauth.resource_ownermap.default:
        class: HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMap
        arguments: ["@security.http_utils", ['default'], ['default']]
    security.user.provider.guardd:
        class: Potf\SecurityBundle\Security\Core\User\GuarddMockUserProvider
    security.user.provider.guardd.user:
        abstract: true
        public: false
        class: Potf\SecurityBundle\Security\Core\User\GuarddUser

hwi_oauth:
    firewall_names: [default]
    resource_owners:
        guardd:
            type:              oauth2
            client_id:         default
            client_secret:     default
            access_token_url:  default
            authorization_url: default
            infos_url:         default
            paths:
                identifier: identifier
                nickname:   nickname
                realname:   realname

security:
    encoders:
        Potf\SecurityBundle\Security\Core\User\GuarddUser: plaintext

    providers:
        in_memory:
            guardd:
                users:
                    user:
                        email: user
                        password: user
                        fullname: user
                        access_token: "eyJhbGciOiJSUzI1NiJ9.eyJmdWxsbmFtZSI6IlVzZXIiLCJ1c2VybmFtZSI6InVzZXIiLCJleHAiOjMzMDQyMTY0MDcxLCJpYXQiOjE0ODUyNTUyNzF9.LQU1NV8FMZGH9b3r7Rn4BrPK5Qany6ym_EyA9MMteImlDrjAs45vIvr0BLKsU4N7isvGGRDc_MXphWUm6pM61LMVTcFOGmlERd1B2BEmWkkDVTKEy5jBu7vOVHqK5LYLseAB4K3Qxk17cDe80uO1vutbeb0syWykuspHuWunr_tTqoot70jYCEmbhvT_4jsxnxowpsoL13iIkkqGdtdLjxTQ5RQE5sjhXnguiHDeC4Fhm1-gsptQmCp00yzdq9GcPFurddtP_MWNjX4NwqhuZjiIVeQqG154ocbF_Z49a6aj2tLsNY_vpinaCcwvNL5Yshg1n96q4JrQZM4U0ysrWE1oPMI887rhHBilwrtk1l1Z5czUH8zVLMzeAzSMr0MvidjM01nKidbbVJBXmaACoyqcftFNAG7_CZijW2oYlZe7UiJ71cpvrxzOr1Tlpgs4YmvxRC4bIwijdI_QJXwDmL4Q-JfRfAu14g9JigEoCHLV-oU56D_e2Btd3X4uL7EII-F--0LKcuFcb1_eK8GTvcECz7rRA5Phpv0Hi4kxJfIQtKFvOnJ3W0JA0X5d_-x2Jn__TdAHrZIVYWChnWKFw2tv7UuQ9ymGoUiq7qH0OSPmFr7DMQtudFE7bszyaK1X_wVhHSFej82HVqL_SVC4epEle5_53xrJC5IuTIgl0sE"
                        actions:
                            - { type: generic, name: 'ROLE_ADMIN' }
                    manager:
                        email: manager
                        password: manager
                        fullname: Manager
                        access_token: "eyJhbGciOiJSUzI1NiJ9.eyJmdWxsbmFtZSI6Ik1hbmFnZXIiLCJ1c2VybmFtZSI6Im1hbmFnZXIiLCJleHAiOjMzMDQyMTY0MzcyLCJpYXQiOjE0ODUyNTU1NzJ9.rtTQ_AK8ZQjJ5LdCSefZrKSiG86hcmpugENHfAXY1vZPvnLQPHSwTqYQbiUkCvoA91Y0CqJpocYFIMVTGGlWLWmjhf7xpuDxUPz2jXvBuvBcITcnPRCBrFh-zxuWoIefrhTm6xxGU8xGLam_ld5WRuxtZeT8DrXSI-g1Ux7YXELJ5HYbvoft6qHYCE85lvjfPJMUGT-A8ig4ycStP-Rn4SIuowqWw5Ap_he-YqsrGv8G8zao_QiyDcvJAqdxCTv8Ts6Pqpa_LFu-Qk1wqqOFv1jRTlwoeUS72FqWmBAhxz8EH1azGMS89xGoVfBOUpWCL6hIS2ossWeFTG2qi2yPRD8Ian8O7hdxo2WKj0AgSlFtzyopHkQ48XQtTq1RWAQvFyt66Pwr4HUh51-lV8UE8q-J2YkW8k06bPLCWCURUe_8-ATDEbDi89A4i3Nv5fyiRJF4OAGR70JWGFmLjx-ASDyh1XuquBNW3aYOodHHPfpIIUEOsEtlZFkyIgDqtDu1D2aMAWNGwVzMpRh0R6pwSn1f_pb_55bq7HyTWSdY1BDC1ZvmLnp-FAjz3ZxQhgvCR79QyMsY1PTAuNeFnALQvoQ6tm_aOZ0dQOblAi3YQAJLL1QcU-Hhf5thtkZ9lDYriEfN69LvPwtvTpWwpymMhJeeGsWwpN4c100r_IM2mnE"
                        actions:
                            - { type: generic, name: 'ROLE_MANAGER' }
                    invalid:
                        email: invalid
                        password: invalid
                        fullname: invalid

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:    ^/login$
            security:   false

        api_doc:
            pattern:    ^/api/doc
            security:   false

        api_exchange:
            pattern:    ^/api/exchange/
            security:   false

        api_area:
            pattern:    ^/api((?!/auth/user).)
            stateless:  true
            guard:
                entry_point: potf_security.guard.jwt
                authenticators:
                    - potf_security.guard.jwt
                    - potf_security.guard.oauth

        secured_area:
            pattern:   ^/
            form_login:
                login_path: guardd_mock_login
                check_path: guardd_mock_login_check
            logout:
                path: logout

    access_control:
        - { path: ^/, roles: ROLE_USER }

1 个答案:

答案 0 :(得分:1)

编辑:原来这是配置文件中的定义问题。

但是,如果您在自动装配AuthorizationChecker课程时遇到问题,则下面的答案仍然有用。

改为使用界面:

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class UserType extends AbstractType
{
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorization = $authorizationChecker;
    }

您还需要symfony/security-bundle中的composer.jsonsymfony/security还不够。