Reputation: 1
I am quite new about using Symfony 2 for developing in PHP, so i used the "Symfony Blog" example as the base of a new simple app in order to try out what i learnt.
I have got this rule defined:
AhorrarMainBundle_cuenta:
pattern: /cuenta/{id}
defaults: { _controller: AhorrarMainBundle:Cuenta:cuenta }
requirements:
_method: GET
id: \d+
So i expect that when i write this address in my browser (ahorro/app_dev/cuenta/1) it launchs "cuentaAction" passing a number 1 as an argument.
This is the code of the twig template i am using as layout:
{% block navigation %}
<nav>
<ul class="navigation">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="{{ path('AhorrarMainBundle_cuenta', {'id': id}) }}">Cuenta</a></li>
<li><a href="{{ path('AhorrarMainBundle_movimientos') }}">Movimientos</a></li>
</ul>
</nav>
{% endblock %}
This is the code of the page launched when calling "ahorro/app_dev/cuenta/1":
{% extends "AhorrarMainBundle::base.html.twig" %}
{% block title %}Cuenta{% endblock%}
{% block body %}
<header>
<h1>Cuenta symAhorro</h1>
</header>
{% endblock %}
Indeed, it is just a simple page i expected to be showed and nothing more. But when i try to access to any page i get this error:
![Error image] http://imageshack.com/a/img837/1190/dg53.jpg
EDITED This is the class i am using as controller:
class CuentaController extends Controller{
public function indexAction()
{
return $this->render('AhorrarMainBundle:MainPage:index.html.twig');
}
public function cuentaAction($id)
{
$em = $this->getDoctrine()->getManager();
$cuenta = $em->getRepository('AhorrarMainBundle:Cuenta')->find($id);
if (!$cuenta) {
throw $this->createNotFoundException('Unable to find Blog post.');
}
return $this->render('AhorrarMainBundle:Cuenta:cuenta.html.twig', array(
'cuenta' => $cuenta,
));
}
}
So seems the problem is that the "id" variable doesnt exist, but i think i followed what the "Symfony book" says about arguments...
Can anyone help me saying how should i do this? or maybe where can i find a working example?
Thanks in advance.
Upvotes: 0
Views: 909
Reputation: 530
What hpoeri commented is one solution to your problem.
Another one (and, usually preferred) would be to return your $id
variable from the controller to the view, since this is basically what the separation between controller and view is about. So your return statement within your controller should read:
return $this->render('AhorrarMainBundle:Cuenta:cuenta.html.twig', array(
'cuenta' => $cuenta,
'id' => $id
));
All variables you pass in the returned array get available in your view.
Have a look at the Symfony2 documentation here, especially the section called "Rendering Tamplates" http://symfony.com/doc/current/book/controller.html#rendering-templates and the section about "Views" which explains variable handling a little bit better here: http://symfony.com/doc/current/quick_tour/the_view.html
Upvotes: 0
Reputation: 36
If you didn't specifically assign the template variable in the controller, it does not automatically exist.
You can get the request parameter named 'id' via the default request variable:
app.request.get( 'id' )
or if you want to use it in several places, assign the variable yourself:
{% set id = app.request.get( 'id' ) %}
Upvotes: 1