KodeFor.Me
KodeFor.Me

Reputation: 13521

Symfony cannot find the template file

I have follow all the steps related to Symfony installation, and I try the examples of the Symfony book (provided by the Symfony web site). Currently I am on Controllers chapter (5) and I try the following code:

namespace MyBundle\FrontBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class HelloController extends Controller
{
    public function indexAction($name, Request $request)
    {
        return $this->redirect($this->generateUrl('front_buy'), 301);
    }

    public function buyAction(Request $request)
    {
        return $this->render(
            'Hello/buy.html.twig',
            array(
                'name' => 'Nikos'
            )
        );
    }
}

but I get the following error:

INFO - Matched route "front_buy" (parameters: "_controller": "MyBundle\FrontBundle\Controller\HelloController::buyAction", "_route": "front_buy")
CRITICAL - Uncaught PHP Exception InvalidArgumentException: "Unable to find template "Hello/buy.html.twig"." at /var/www/projects/symfony/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 128 

Context: {"exception":"Object(InvalidArgumentException)"}

I know is not something fancy, but I don't know how to fix the problem.

My view file is under the following path : ./src/MyBundle/FrontBundle/Resources/views/Hello/buy.html.twig.

Upvotes: 4

Views: 8357

Answers (3)

Bourados
Bourados

Reputation: 47

In my case, the problem was really related to the specific version of Symfony. My project used to be run with Symfony 2.8.19. I tried to migrate it to another Linux evironment (uning symfony2.8.42) and then I got the very same error with the same code (very wierd).

I tried the solutions mentioned before and none worked like using : $this->render('@BlogBundle/Blog/index.html.twig') or $this->render('AcmeBlogBundle:Blog:index.html.twig').

After several tries, I found the problem. In fact, in my old env, I had :

return $this->render('XxxxxBundle:Default:zzzz.html.twig';

But the problem was with Capitalized Default because the real path was with an uncapitalized default

so changing it to the following fixed the problem in my new env:

return $this->render('XxxxxBundle:default:zzzz.html.twig';

Upvotes: 0

Pasindu Dilshan
Pasindu Dilshan

Reputation: 386

in "app/config/config.yml" be sure to have this:

framework:
# ...
templating:
  engines: ['twig']

Upvotes: 0

KhorneHoly
KhorneHoly

Reputation: 4766

You need to render it with the following syntax:

$this->render('AcmeBlogBundle:Blog:index.html.twig')

or with this syntax

$this->render('@BlogBundle/Blog/index.html.twig')

An Example:

$this->render('@FrontBundle/Hello/buy.html.twig) 

More information can be found on the documentation, also refering to symfonys best practices templates should be stored in app/Ressources/views

Upvotes: 7

Related Questions