0

I have the authentification controller

public function indexAction(Request $request)
{

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('Mql14mqlmeBundle:User');

   if ('POST' === $request->getMethod()) {
        $username = $request->get('login');
    $password = $request->get('pass');
          if(($username=='admin')&&($password=='admin')){

           return $this->redirect($this->generateUrl('mql14mqlme_admin',  array('name' => 'welcome')));
            }

    $user = $repository->findOneBy(array('login' => $username, 'pass' =>  $password));

    if ($user) {

        return $this->redirect($this->generateUrl('mql14mqlme_acceuil', array('name' => $user->getNom(),
                'id'=> $user->getId(),
            )));


    }else {
      return $this->redirect($this->generateUrl('mql14mqlme_homepage', array('name' =>$username)));
  }

If the user has provided the right login and password he is redirected to acceuil twig, in this twig I want to get the user's id to use it in another twig, so the line where I have a problem is this:

<a href="{{ path('mql14mqlme_interet', { 'id': name   }) }}">INTERETS</a>

the error I get is:Variable "name" does not exist in Mql14mqlmeBundle:Default:acceuil.html.twig at line 89

The action code for acceuil:

public function acceuilAction()
{

$em = $this->container->get('doctrine')->getEntityManager();
$evenements= $em->getRepository('Mql14mqlmeBundle:Evenement')->findAll();
$categories= $em->getRepository('Mql14mqlmeBundle:Categorie')->findAll();
return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Default:acceuil.html.twig',
        array(
                'evenements' => $evenements,
                'categories' => $categories
        ));

}   
2
  • Can you add your PHP code where you render the twig template? Commented Jun 13, 2014 at 19:31
  • 1
    Could you post the action code for mql14mqlme_acceuil? Commented Jun 13, 2014 at 19:31

3 Answers 3

4

Try :

<a href="{{ path('mql14mqlme_interet', { 'id': app.request.get('name') }) }}">INTERETS</a>

Question :

Does your

$user = $repository->findOneBy(array('login' => $username, 'pass' => $password));

indicates that your looking for user by plain password ?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you david your suggestion works perfectly, I'm able to pass the variable into the twig
0

In twig, all variables need to be passed in when you call render. EG:

echo $template->render(array('name' => 'bob'));

6 Comments

so it's not possible to get a variable that was passed in the URL?
Not in the template, no. You could probably write your own twig function to pull a get parameter, but that would be far more roundabout than just passing in the var from the controller. Are you not able to modify that controller?
in the index action instead of redirect I did this :return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Default:acceuil.html.twig',array('user' => $user, ));But I think that it's not possible to render a twig from two different controller if I'm not mistaken
You have the variable array in that line! Just add name to array('user' => $user, ) and it should work
From a twig template you can call a controller : {{ render(controller('NsMyBundle:Default:action')) }} or {{ render(controller('NsMyBundle:Default:action', { parameter: value })) }}
|
0

What you need is getting the name from the query string and pass the name to the rendered view

public function acceuilAction($name) // <-- This is the name you are passing to request
{
    ...
    return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Default:acceuil.html.twig',
    array(
            'name' => $name, // <-- Here you pass the received name to your twig
            'evenements' => $evenements,
            'categories' => $categories
    ));
}

Base on documentation generating URL and passing arguments as array, means you pass the name through the request. If you want to pass as QueryString (not a part of url path) change the generate URL as

$this->redirect($this->generateUrl('mql14mqlme_acceuil').'?name='.$user->getId());

In this case you need to change routing and controller, too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.