Spoon
Spoon

Reputation: 41

Symfony 2 Call a function(with temlate render) in a Controller

i have two simple functions inside my controller:

    public function indexAction(){

    $userid = $this->getUser()->getId();

    $userdata = $this->getDoctrine()
            ->getRepository('LiveupUserBundle:userData')
            ->findOneById($userid);

    $userfriends = $this->getDoctrine()
            ->getRepository('LiveupUserBundle:userFriends')
            ->findByUser($userid);
    return $this->render('LiveupMainBundle:Main:profile.html.twig', array(
        'userdata' => $userdata,
        'userfriends' => $userfriends
    ));
}

and

    public function peopleAction($nick){
    if($nick){

        $frienddata = $this->getDoctrine()
                ->getRepository('LiveupUserBundle:userData')
                ->findOneByNick($nick);

        if($frienddata->getId() === $this->getUser()->getId())
        {
            self::indexAction();
        }else{
            $friendfriends = $this->getDoctrine()
                    ->getRepository('LiveupUserBundle:userFriends')
                    ->findByUser($frienddata->getId());

            return $this->render('LiveupMainBundle:Main:people_profile.html.twig', array(
                'frienddata' => $frienddata,
                'friendfriends' => $friendfriends
            ));
        }
    }
}

Problem is when if statement in second function is true, I want to execute indexAction and render template from that function(profile.html.twig) but I get no response error. Can anyone help me? Thanks in advance.

Upvotes: 2

Views: 312

Answers (2)

Sehael
Sehael

Reputation: 3736

I think what you are looking for is forwarding a request. From the docs, you can do something like this:

public function peopleAction($nick){
    if($nick){

        $frienddata = $this->getDoctrine()
            ->getRepository('LiveupUserBundle:userData')
            ->findOneByNick($nick);

        if($frienddata->getId() === $this->getUser()->getId())
        {
            $response = $this->forward('AppBundle:Something:index', array(
                 'var1'  => $anyVar //if you need to forward a param
            ));
        }else{
            $friendfriends = $this->getDoctrine()
                ->getRepository('LiveupUserBundle:userFriends')
                ->findByUser($frienddata->getId());

            $response = $this->render('LiveupMainBundle:Main:people_profile.html.twig', array(
                'frienddata' => $frienddata,
                'friendfriends' => $friendfriends
            ));
        }

        return $response;
    }
}

Also, by looking at your code, you will still get an error if your $nick variable evaluates to false because no response will be returned. You should ensure that you are ALWAYS returning a response from a controller.

Another thing to think about, maybe redirecting will meet your needs as well. look at the docs to see how to do that, it is pretty simple from a controller.

Upvotes: 1

origaminal
origaminal

Reputation: 2075

You forgot return statement:

return self::indexAction();

Also if nick can be empty you should process this case and throw an exception or return Response.

Upvotes: 0

Related Questions