0

I am using Symfony 3.4.4 and getting Twig_Error_Loader:

Unable to find template "MyBundle:Default:index.html.twig" (looked into: /Users/nemanja/sites/SimfoniAplikacija/app/Resources/views, /Users/nemanja/sites/SimfoniAplikacija/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).

This is my default controller in MyBundle/Controller

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/my", name="mypage")
     */
    public function indexAction()
    {
        return $this->render('MyBundle:Default:index.html.twig');
    }
}

And my index file is where it says: MyBundle/Default/index.html.twig

I tried to change return $this->render('MyBundle:Default:index.html.twig'); to return $this->render('MyBundle/Default/index.html.twig'); But I still get the same error. It can't find index template.

Nemus
  • 3,879
  • 12
  • 38
  • 57
  • Missing the @ symbol which idicates a twig namespace. https://stackoverflow.com/questions/47832977/symfony-3-4-use-view-inside-my-bundle/47835716#47835716 – Cerad Mar 05 '18 at 19:14
  • When I do that, I get: There are no registered paths for namespace "MyBundle"....But I found the namespace with bin/console debug:twig command – Nemus Mar 06 '18 at 13:17

2 Answers2

0

First I tried to add @ symbol in front of bundle name:

return $this->render('@MyBundle/Default/index.html.twig');

But than I get an error:

There are no registered paths for namespace "MyBundle".

To find out the namespace, I run:

bin/console debug:twig

This listed

...

 @My             - src/MyBundle/Resources/views                                                 
 @!My            - src/MyBundle/Resources/views   

...  

And than I added in my controller:

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/my", name="mypage")
     */
    public function indexAction()
    {
        return $this->render('@My/Default/index.html.twig');
    }
}

And now it can find index template at the specified route.

Nemus
  • 3,879
  • 12
  • 38
  • 57
-1

Your template Path should be:

/Users/nemanja/sites/SimfoniAplikacija/app/Resources/views/index.html.twig

Regards