3

im new to symfony framework. it raises render error. i tried all suggestions

@App/default/index.html.twig
AppBundle:default:index.html.twig
default/index.html.twig

routing.yml

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

lucky_number:
    path:     /lucky/number/{count}
    defaults: { _controller: AppBundle:Lucky:number }

LuckyController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LuckyController
{
    /**
     * @Route("/lucky/number/{count}")
     */
    public function numberAction($count)
    {
        $number = rand(0, 100);
        $html = $this->render('@App/default/index.html.twig','number'=>$number);
        //$html = $this->container->get('templating')->render('AppBundle:default:index.html.twig',array('number' => $number));
        //$html = $this->container->get('templating')->render('default/index.html.twig',array('number' => $number));
        return new Response($html);
    }
}

Error:

Attempted to call an undefined method named "render" of class "AppBundle\Controller\LuckyController".

1 Answer 1

18

You forgot to extend your controller class with the symfony controller.

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LuckyController extends Controller // <-- HERE
{
    /**
     * @Route("/lucky/number/{count}")
     */
    public function numberAction($count)
    {
        $number = rand(0, 100);
        return $this->render('AppBundle:default:index.html.twig',array('number' => $number));
    }
}
Sign up to request clarification or add additional context in comments.

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.