Take for example the following controller/action:
public function indexAction()
{
return $this->render('TestBundle:TestController:index.html.twig');
}
I would like to write the template expression (or whatever it’s name is) this way:
public function indexAction()
{
return $this->render('*:TestController:index.html.twig');
}
So that symfony knows I’m looking for a template in this very bundle. Having to write the whole Owner + Bundle for every template/action/repository I want to refer is very annoying. Even more so considering most of the time I refer to actions and templates in the same bundle.
NOTE: I know templates can be put at the app level and be refernced like this:
'::index.html.twig'
But that is not what I need.
It’s possible with a bit of custom code.
Basically, you want to override the controller’s
render()method and include logic to fetch the name of the current bundle.Note that instead of my controllers extending
Symfony\Bundle\FrameworkBundle\Controller\Controller, they extend a custom controller (which then extends Symfony’s controller). This allows you to conveniently give the controller more ability by adding your own methods.Ex:
MyBundle\Controller\MyController\extendsMyCustomBaseControllerwhich extendsSymfony\Bundle\FrameworkBundle\Controller\Controller.So, in my custom controller I have these two methods:
Take a look at
render(). It fetches the current bundle name and uses it to build the$viewvariable. Then it just callsparent::render()and it’s as if you had manually defined the bundle name in the render statement.The code here is very simple, so you should be able to easily extend it to do other things, such as allow you to also avoid typing the controller name.
Important: If you do use a custom controller, make sure you
use Symfony\Component\HttpFoundation\Response, otherwise PHP will complain that the method signatures forrender()don’t match.