symfony 属性语法注解:方法App\Controller\...中的注解“@Security”从来没有进口过

4c8rllxm  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(91)

通过将我的应用从Sf5切换到Sf6,我有属性问题的注解:

[Semantical Error] The annotation "@Security" in method App\Controller\MyController::delete() was never imported. Did you maybe forget to add a "use" statement for this annotation? in {"path":"..\/..\/src\/Controller\/","namespace":"App\\Controller"} (which is being imported from "/var/www/config/routes/attributes.yaml"). Make sure there is a loader supporting the "attribute" type.
axzmvihb

axzmvihb1#

第一步是将config/routes.yaml移动到config/routes/attributes.yaml,并更新控制器路径(添加 ../),如www.example.com所述https://symfony.com/doc/current/routing.html#creating-routes-as-attributes
第二步是将安全注解转换为属性。我使用rector将routes注解转换为属性,但我没有找到安全规则,所以做如下操作:

// annotation :
    /**
     * @Security("is_granted('ROLE_ADMIN')")
     */
// --> attribute :
    #[IsGranted('ROLE_ADMIN')]

我用的是vim(neovim)rexeg

%s/\(\s*\)\/\*\*\n\s*\* @Security("is_granted\(('ROLE_.*')\)")\n\s*\*\//\1#[IsGranted\2]/cg

我删除了FrameworkExtraBundle上的use:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

并删除了已弃用的FrameworkExtraBundle:
composer remove sensio/framework-extra-bundle
错误就消失了

相关问题