php - Variable "name" does not exist in UsersListBundle:Default:index.html.twig at line 1 - Symfony 2 -
i getting error using symfony 2 while fetching users database:
variable "name" not exist in userslistbundle:default:index.html.twig @ 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> homepage</p> <p><a href="users/add">add new user</a></p> <p>all users in users table using: <br /> <strong>$repository = $this->getdoctrine()->getrepository('userslistbundle:users');<br /> $users = $repository->findall();</strong></p> {% if users 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> {% 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 database</p> {% endif %} <p>get id 1 in example table using: <br /> <strong>$user = $this->getdoctrine()->getrepository('userslistbundle:users')->find(1);</strong></p> </div> </div> </body> </html>
here controller code:
/** * @route("/users") * @template() */ public function indexaction() { $repository = $this->getdoctrine() ->getrepository('userslistbundle:users'); $users = $repository->findall(); return array('users' => $users); }
i have read articles error. hidden character. character occurs when hit alt gr + space. dont know how remove character. tried retyping same code no luck.
here user entity:
<?php // src/users/bundle/listbundle/entity/users.php namespace users\bundle\listbundle\entity; use doctrine\orm\mapping 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; /** * 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; } /** * 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; } /** * 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; } /** * phone * * @return string */ public function getphone() { return $this->phone; } }
here 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 system throw $this->createnotfoundexception( 'no user found 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 system throw $this->createnotfoundexception( 'no user found id '.$id ); } else { $em->remove($user); $em->flush(); return $this->redirect($this->generateurl('users_list_users_index'), 301); } } }
you have syntax issue on line :
{% if users not empty %)
replace %) %}
Comments
Post a Comment