Hey, is it possible to refactor Template() annotation to render method in Symfony?
Before
/**
* @Template()
* @Route("/index", name="_index")
*/
public function indexAction()
{
return [];
}
After
/**
* @Route("/index", name="_index")
*/
public function indexAction()
{
return $this->render('index.html.twig');
}
@mssimi Are you using a specific version of Symfony?
3.4
Hey, we can add this feature.
Could you provide more complex use cases so we add them to tests?
The template name 'index.html.twig' is derived from indexAction() method name?
Sorry I actually made minor mistake in first example, here is better example.
use case 1 before
class AppController extends Controller
{
/**
* @Template()
* @Route("/index", name="_index")
*/
public function indexAction()
{
return [];
}
}
use case 1 after
class AppController extends Controller
{
/**
* @Route("/index", name="_index")
*/
public function indexAction()
{
return $this->render('AppBundle:App:index.html.twig');
}
}
use case 2 before
class AppController extends Controller
{
/**
* @Template('AppBundle:App:detail.html.twig')
* @Route("/index", name="_index")
*/
public function indexAction()
{
return ['name' => 'Rector'];
}
}
use case 2 after
class AppController extends Controller
{
/**
* @Route("/index", name="_index")
*/
public function indexAction()
{
return $this->render('AppBundle:App:index.html.twig', ['name' => 'Rector']);
}
}
Here is documentation for Template annotation on Symfony.com with few more examples.
If Template() is empty template is derived from Controller class name and from method name.
Thanks! That looks something that could be automated.
Just wondering, why did you decide to go this way?
Just a tip, you can use ```php to highlight the code for PHP (I've added it to the code, so you cansee it).
It is not recommend for some time already and I can typehint it better.
https://symfony.com/doc/current/best_practices/controllers.html#template-configuration
I also want to refactor my template locations and migrate to bundles-less Application. At the moment I maintain multiple projects where I have bundles like AppBundle and ShopBundle, I want to move all templates from Bundle/Resources/views to template dir in root of project and templates from ShopBundle to template/admin dir. I want to automatize that as much as I can.
I think rector to refactor template locations would be rly great to migrate to bundle less apps and flex.
Thanks for the examples. They're super clear
I must inform you the Rector for this is under construction :+1: :)
Most helpful comment
Thanks for the examples. They're super clear
I must inform you the Rector for this is under construction :+1: :)