Search code examples
twigsymfony4

Unable to pass data to twig file from controller in symfony


I am trying to pass data from the controller to view, but when I passed the data am getting the following error

Variable "products" does not exist.

PLease find my controller code below

<?php

namespace App\Controller;

USE App\Entity\Product;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

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

    public function show(EntityManagerInterface $entityManager): Response
    {
        $products = $entityManager->getRepository(Product::class)->findAll();

        if (!$products) {
            throw $this->createNotFoundException(
                'No product found for id '.$id
            );
        }

        else{
           return $this->render('product/index.html.twig', ['product' => $products]);
        }

        

    }
}

Please find my twig template code below

<form action="{{ path('home') }}">
                                <fieldset>
                                    <label class="all-label">Product</label>
                                    <div class="input-group mb-3 select-down">
                                        <select class="form-select form-control" aria-label="Default select example" name="product">
                                           
                                                {% for product in products %}
                                                    <option value="{{ product.id }}">{{ product.name }}</option>
                                                {% endfor %}
                                           
                                        </select>
                                    </div>
                                </fieldset>
</form>

Codes in routes.yaml file

controllers:
    resource:
        path: ../src/Controller/
        namespace: App\Controller
    type: attribute
home:
  path: /
  controller: App\Controller\HomeController::index
create:
  path: /create
  controller: App\Controller\MainController::create
test:
  path: /xavier
  controller: App\Controller\HomeController::index
product:
  path: /product
  controller: App\Controller\ProductController::index

Why am getting this error, when i tried to vardump the value of $product am gettinng data also. Why it is showing Variable "products" does not exist error

Am trying to load the page by

  http://127.0.0.1:8000/product 

Solution

  • There is your answer:

    • you are calling this route /product

    • your config here says use the ProductController with the index() method:

    product:
      path: /product
      controller: App\Controller\ProductController::index
    

    the index controller renders the twig template with the following variables (controller_name):

    return $this->render('product/index.html.twig', [
        'controller_name' => 'ProductController',
    ]);
    

    solution:

    change your product route to the show method to call the correct controller method:

    product:
      path: /product
      controller: App\Controller\ProductController::show
    

    or put "products" as variable in the index() method:

        //#[Route('/product', name: 'app_product')]
        public function index(): Response
        {
            $products = []; // insert products here 
    
            return $this->render('product/index.html.twig', [
                'controller_name' => 'ProductController',
                'products' => $products,
            ]);
        }