So I'm brand new to symfony (and MVC frameworks in general) so I'll need a complete beginners answer to my question please.
Basically, I've set up a controller to add a class to the navigation element of the current page. At least in theory I have, in practice I get the following exception error:
An exception has been thrown during the rendering of a template ("The controller must return a response (Text elements given).") in "myNewBundle:Page:text-elements.html.twig".
I think that the problem (or at least part of it) is that the controller has been decoupled from the template. So it has no idea if the page it is being called on, is current or not.
Here is the contents of my controller:
<?php
namespace my\NewBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* @Template("myNewBundle::definitions.html.twig")
*/
class NavController extends Controller
{
public function renderNavAction($target='/usage', $text='Insert Link')
{
$output = '<a href="' . $this->generateUrl($target) . '" ';
if ($this->getRequest()->get('_route') == $target) $output .= 'class="active"';
$output .= '>' . $text . '</a>';
return $output;
}
}
And this is the part of the twig template which should render it out:
<ul>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_textElements', 'text' : 'Text elements' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_buttons', 'text': 'Buttons' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_forms', 'text': 'Forms' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_lists', 'text': 'Lists' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_tables', 'text': 'Tables' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_searchBoxes', 'text': 'Search Boxes' })) }}</li>
<li>{{ render(controller('myNewBundle:Nav:renderNav', { 'target': 'my_new_pods', 'text': 'Reusable Pods' })) }}</li>
</ul>
Could somebody please let me know what it is that I'm doing wrong? Note: The template annotations part is something I added when trying to resolve the issue myself. If it's not necessary then I'm happy to remove/alter it.