简体   繁体   中英

Variable “name” does not exist in UsersListBundle:Default:index.html.twig at line 1 - Symfony 2

I am getting this error using Symfony 2 while fetching all users from database:

 Variable "name" does not exist in UsersListBundle:Default:index.html.twig at line 1
500 Internal Server Error - Twig_Error_Runtime 

My code: index.html.twig

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Symfony 2 Example</title>
<link href="/bundles/userslist/css/main.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div id="container">
    <h1>User list page</h1>
    <div id="body">
        <p><a href="/">Click here</a> to back to the Homepage</p>
        <p><a href="users/add">ADD NEW USER</a></p>
        <p>All users in the users table using: <br />
        <strong>$repository = $this->getDoctrine()->getRepository('UsersListBundle:Users');<br />
        $users = $repository->findAll();</strong></p>
        {% if users is not empty %)
            <table cellpadding="4">
                <tr>
                    <td><strong>ID</strong></td>
                    <td><strong>Name</strong></td>
                    <td><strong>Email</strong></td>
                    <td><strong>Phone</strong></td>
                </tr>
                {% for user in users %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.name }}</td>
                        <td>{{ user.email }}</td>
                        <td>{{ user.phone }}</td>
                        <td><a href="users/edit/{{ user.id }}">Edit</a></td>
                        <td><a href="users/delete/{{ user.id }}">Delete</a></td>
                    </tr>
                {% endfor %}
            </table>
        {% else %}
            <p>No users in the database</p>
        {% endif %}
        <p>Get ID 1 in the example table using: <br />
        <strong>$user = $this->getDoctrine()->getRepository('UsersListBundle:Users')->find(1);</strong></p>
    </div>
</div>
</body>
</html>

Here is my controller code:

/**
     * @Route("/users")
     * @Template()
     */
    public function indexAction()
    {
        $repository = $this->getDoctrine()
            ->getRepository('UsersListBundle:Users');

        $users = $repository->findAll();

        return array('users' => $users);
    }

I have read some articles about this error. They say something about a hidden character. This character occurs when you hit Alt Gr + Space. Now I dont know how to remove that character. I tried retyping same code but no luck.

Here is my User Entity:

<?php
// src/Users/Bundle/ListBundle/Entity/Users.php

namespace Users\Bundle\ListBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class Users
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=200)
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=60)
     */
    protected $phone;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Users
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Users
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set phone
     *
     * @param string $phone
     * @return Users
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string 
     */
    public function getPhone()
    {
        return $this->phone;
    }
}

Here is my UserController:

<?php

namespace Users\Bundle\ListBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Users\Bundle\ListBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Request;

class UsersController extends Controller
{
    /**
     * @Route("/users")
     * @Template()
     */
    public function indexAction()
    {
        $repository = $this->getDoctrine()
            ->getRepository('UsersListBundle:Users');

        $users = $repository->findAll();

        return array('users' => $users);
    }

    /**
     * @Route("/users/add")
     * @Template()
     */
    public function addAction(Request $request)
    {

        $user= new Users();
        $user->setName('Name');
        $user->setEmail('Email');
        $user->setPhone('Phone');

        $form=$this->createFormBuilder($user)
            ->add('name')
            ->add('email')
            ->add('phone')
            ->getForm();

        if ($request->getMethod()=='POST'){
            $form->bind($request);
            if ($form->isValid()){
                $em = $this->getDoctrine()->getManager();
                $em->persist($user);
                $em->flush();
                return $this->redirect($this->generateUrl('users_list_users_index'), 301);
            }
        } else {
            return $this->render('UsersListBundle:Users:add.html.twig',array(
                'form'=>$form->createView(),
            ));
        }
    }

    /**
     * @Route("/users/edit/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
     * @Template()
     */
    public function editAction($id, Request $request)
    {
        if ($id == 0){ // no user id entered
            return $this->redirect($this->generateUrl('users_list_users_index'), 301);
        }
        $user = $this->getDoctrine()
            ->getRepository('UsersListBundle:Users')
            ->find($id);

        if (!$user) {// no user in the system
            throw $this->createNotFoundException(
                'No user found for id '.$id
            );
        }

        $form=$this->createFormBuilder($user)
            ->add('name')
            ->add('email')
            ->add('phone')
            ->getForm();

        if ($request->getMethod()=='POST'){
            $form->bind($request);
            if ($form->isValid()){
                $em = $this->getDoctrine()->getManager();
                $em->persist($user);
                $em->flush();
                return $this->redirect($this->generateUrl('users_list_users_index'), 301);
            }
        } else {
            return $this->render('UsersListBundle:Users:edit.html.twig',array(
                'form'=>$form->createView(),
            ));
        }

        return array('name' => $id);
    }

    /**
     * @Route("/users/delete/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
     * @Template()
     */
    public function deleteAction($id)
    {
        if ($id == 0){ // no user id entered
            return $this->redirect($this->generateUrl('users_list_users_index'), 301);
        }

        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository('UsersListBundle:Users')->find($id);

        if (!$user) { // no user in the system
            throw $this->createNotFoundException(
                'No user found for id '.$id
            );
        } else {
            $em->remove($user);
            $em->flush();
            return $this->redirect($this->generateUrl('users_list_users_index'), 301);
        }
    }
}

You have an syntax issue on this line :

{% if users is not empty %)

Replace %) by %}

Maybe do not have such field name. Are this error on line 1 at:

   <!DOCTYPE html>

or at:

   <td>{{ user.name }}</td>

try to delete second and check if still have error.

could you show me in twig dump of users or UsersListBundle:Users entity

  .....
  {% if users is not empty %)
  {% dump(users) %}
  .....

If you added or renamed the 'name' field in the database, you have to regenerate your entity. Adding the Users entity to your question would help you get a faster response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM