0

I'm working with Symfony 2 and I want to pass from my controller to my twig template a simple string and then use it on my template to descriminate the user role. The controller code has something like :

public function modify_user_asAction(Request $request, $username)
{
    $stringtopass="admin";
    $um = $this->get('fos_user.user_manager');
    $user = $um->findUserByUsername($username);
    if($user == null){
        //error page here..
    }
    $form = $this->createForm(new UserForm(), $user);
    $form->handleRequest($request);
    if ($form->isValid()) {     
        $um->updateUser($user);
        return $this->redirect($this->generateUrl('acme_query_success'));
    }
     return $this->render('AcmeUserBundle:Default:modifyuserform.html.twig', array(
        'form' => $form->createView(),
    ));
}

I want to pass $stringtopass in the generateUrl (if it's possible). I can't find anything online. Thanks

3
  • Can you please provide the whole action, or at least the return statement ? Commented Nov 28, 2013 at 10:59
  • I don't know what you really are trying to do, but if you rely on a client side input to guess the user role, it sound like a nice security issue Commented Nov 28, 2013 at 11:05
  • If a user try to access ^/admin or ^/sadmin only and he's not logged in as admin or super_admin he's gonna be redirected to login page Commented Nov 28, 2013 at 11:17

1 Answer 1

1

You are almost there!

API: generateUrl

Basically, just pass an array as second param to generateUrl.

return $this->redirect($this->generateUrl('acme_query_success', array('stringToPass' => $stringtopass)));

And also, @Brewal has a very valid point there. Be careful not to pass some sensitive data or leave unrestricted access to that controller's action. You could do more harm than good...

UPDATE:

public function acmeQuerySuccessAction(){
    // ... action's logic
    $stringToPass = $this->getRequest()->query->get('stringToPass');
    // .....

    return array(
          'stringToPass' => $stringToPass,
          // all other elements that you would normally return
    );
}  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! And how can I use it in twig file?
That is whole another thing. When you redirect to acme_query_success it call another action (let's call it acmeQuerySuccessAction()). In there you separately forward request data to twig. Updating my answer with appropriate sample...

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.