Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Render a post if is_verified = true in Symfony Controller

I’m building an app with Symfony, and I would like to render a post only if is_verified = true but I don’t know where to put this condition in the controller and to access the property (is_verified). Please someone to help me.

Controller.php :

#[Route('/annonce')]
class AnnonceController extends AbstractController
{
#[Route('/', name: 'app_annonce_index', methods: ['GET'])]
public function index(AnnonceRepository $annonceRepository): Response
{
        return $this->render('annonce/index.html.twig', [
            'annonces' => $annonceRepository->findAll(),
        ]);
}

#[Route('/new', name: 'app_annonce_new', methods: ['GET', 'POST'])]
public function new(Request $request, AnnonceRepository $annonceRepository): Response
{
    $annonce = new Annonce();
    $form = $this->createForm(AnnonceType::class, $annonce);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $annonceRepository->add($annonce, true);

        return $this->redirectToRoute('app_annonce_index', [], Response::HTTP_SEE_OTHER);
    }

    return $this->renderForm('annonce/new.html.twig', [
        'annonce' => $annonce,
        'form' => $form,
    ]);
}

#[Route('/{id}', name: 'app_annonce_show', methods: ['GET'])]
public function show(Annonce $annonce): Response
{
    return $this->render('annonce/show.html.twig', [
        'annonce' => $annonce,
    ]);
}

The entity file :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#[ORM\Entity(repositoryClass: AnnonceRepository::class)]
class Annonce
{

#[ORM\Column]
private ?bool $is_verified = false;


public function getIsVerified(): ?bool
{
    return $this->is_verified;
}

public function setIsVerified(bool $is_verified): self
{
    $this->is_verified = $is_verified;

    return $this;
}
}

Thank you for any help

>Solution :

You could do this in your show method:

#[Route('/{id}', name: 'app_annonce_show', methods: ['GET'])]
public function show(Annonce $annonce): Response
{
    if (!$annonce->getIsVerified()) {
        throw $this->createNotFoundException();
    }

    return $this->render('annonce/show.html.twig', [
        'annonce' => $annonce,
    ]);
}

and this in your index method:

#[Route('/', name: 'app_annonce_index', methods: ['GET'])]
public function index(AnnonceRepository $annonceRepository): Response
{
    return $this->render('annonce/index.html.twig', [
        'annonces' => $annonceRepository->findBy(['is_verified' => true]),
    ]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading