在Symfony 2.8中使用SensioFrameworkExtraBundle @Security和@IsGranted时遇到麻烦

时间:2019-02-15 11:28:35

标签: routing symfony-2.8

所以我想实现一项功能,该功能可以检查特定角色是否可以访问路由(URL),或者不使用SensioFrameworkExtraBundle @ Security&@ IsGranted从此处https://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/security.html

和即时通讯使用symfony 2.8

当我在控制器中添加IsGranted时,出现此错误消息

  

[语义错误] AppBundle \ Controller \ ProductController :: newAction()方法中的注释“ @Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ IsGranted”不存在,或者无法自动加载到/ home / lavin / mini-project / src / AppBundle / Controller /(从“ /home/lavin/mini-project/app/config/routing.yml”导入)。

所以我尝试在堆栈溢出中搜索一些未解决的问题,但是其他线程中的某些答案仍然让我感到困惑,无法像该线程那样解决此问题 Creating a custom requirement validator for @Route annotation in Symfony

和这个 symfony2: check isGranted for a route

这是我的控制器代码

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use AppBundle\Entity\Product;
use AppBundle\Entity\Category;
use AppBundle\Entity\Cart;
use AppBundle\Entity\CartProduct;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class ProductController extends Controller
{
    /**
     * @Route("/home")
     */
    public function homeAction()
    {
        return $this->render('product/home.html.twig');
    }
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', array(
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
        ));
    }

    /**
     * @Route("product/new", name="product_new")
     * 
     * @IsGranted("ROLE_SELLER")
     */
    public function newAction(Request $request)

所以我希望只有具有ROLE_SELLER的用户和具有ROLE_CUSTOMER无法打开或访问的用户才能访问路由器URL“ / product / new”

0 个答案:

没有答案