Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 5 PHP8 Attributes for Security

I have a page that I am trying to convert from annotations to PHP8 attributes.

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[IsGranted('ROLE_ADMIN')]
#[Route('/page')]
class PageController extends AbstractController
{
    #[Route('/', name: 'page')]
    public function index(): Response
    {
        return $this->render('page/index.html.twig', [
            'controller_name' => 'PageController',
        ]);
    }
}

The IsGranted attribute doesn't seem to work, as the page is accessible instead of a 403 error. On the other hand when converted to annotations, like below it works as expected. Is there a config setting I am missing?

/**
 * @IsGranted("ROLE_ADMIN")
 */
#[Route('/page')]
class PageController extends AbstractController
{
    #[Route('/', name: 'page')]
    public function index(): Response
    {
        return $this->render('page/index.html.twig', [
            'controller_name' => 'PageController',
        ]);
    }
}

Other attributes eg #[Route], #[Entity] etc works, but the Security attributes do not seem to work.

like image 449
Patrick Kenekayoro Avatar asked Oct 20 '25 16:10

Patrick Kenekayoro


1 Answers

Support for PHP8 attributes in the SensioFrameworkExtraBundle package are available only from version 6.1.0. You likely just need to update it.

like image 121
Federkun Avatar answered Oct 23 '25 07:10

Federkun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!