Reputation: 23
I'm developing an app with Symfony 4 but I'm having a little problem here.
I made a controller with a required parameter but when I to follow the route I get this message:
Here's the code of is my html file:
{% extends 'base.html.twig' %}
{% block title %}Registro Madre{% endblock %}
{% block body %}
<div class="container">
<h1>Ingrese los datos de la madre</h1>
<form action="/insertMadre" method="POST">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="nombre">Nombres:</label>
<input type="text" name="nombre" class="form-control" id="nombre" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="apellido">Apellidos:</label>
<input type="text" name="apellido" class="form-control" id="apellido" required>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<div class="form-group">
<label for="nacimiento">Fecha de nacimiento</label>
<input type="date" name="nacimiento" id="nacimiento" class="form-control" required>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="nacionalidad">Nacionalidad</label>
<input type="text" name="nacionalidad" id="nacionalidad" class="form-control" required>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="contacto">Teléfono de Contacto</label>
<input type="tel" name="contacto" id="contacto" class="form-control" required>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="dui">DUI (sin guiones)/No. Pasaporte</label>
<input type="text" name="dui" id="dui" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="direccion">Dirección:</label>
<input type="text" name="direccion" id="direccion" class="form-control" required>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="departamento">Departamento:</label>
<select name="departamento" id="departamento" class="form-control" required>
<option value="">Seleccione una opción..</option>
{% for departamento in departamentos %}
<option value="{{ departamento.id }}">{{ departamento.depname }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="municipio">Municipio:</label>
<select name="municipio" id="municipio" class="form-control" required>
<option value="">Seleccione una opción..</option>
</select>
</div>
</div>
</div>
<legend>Datos Clínicos</legend>
<div id="datos-clinicos">
<div class="row">
<div class="col-sm-2">
<span class="datos-clinicos-text">Fórmula Obstétrica</span>
</div>
<div class="col-sm-1">
<span class="datos-clinicos-text">G</span>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" min="0" step="1" value="0" name="formG" id="formG">
</div>
<div class="col-sm-1">
<span class="datos-clinicos-text">P</span>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" min="0" step="1" value="0" name="formP" id="formP">
</div>
<div class="col-sm-1">
<span class="datos-clinicos-text">P</span>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" min="0" step="1" value="0" name="formP1" id="formP1">
</div>
<div class="col-sm-1">
<span class="datos-clinicos-text">A</span>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" min="0" step="1" value="0" name="formA" id="formA">
</div>
<div class="col-sm-1">
<span class="datos-clinicos-text">V</span>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" min="0" step="1" value="0" name="formV" id="formV">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="enfermedadMedica">Enfermedades médicas que afecten el desarrollo del embarazo</label>
<select name="enfermedadMedica" id="enfermedadMedica" class="form-control">
<option value="0">Elija una opción</option>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="enfermedadExantematica">Enfermedad exantemática durante el embarazo</label>
<select name="enfermedadExantematica" id="enfermedadExantematica" class="form-control">
<option value="0">Elija una opción</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="informacionExtra">Otra Información</label>
<textarea name="informacionExtra" id="informacionExtra" class="form-control" rows="5" style="resize:none"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 text-right">
<input type="submit" value="Registrar" class="btn-site">
</div>
</div>
</form>
</div>
{% endblock %}
Here's my controller:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Madre;
use App\Entity\DatosClinicosMadre;
use App\Entity\ProductoObstetrico;
use App\Entity\CondicionProducto;
class ProductoController extends AbstractController
{
public function registro($id)
{
$madre = $this->getDoctrine()->getRepository(Madre::class)->find($id);
return $this->render('producto/register.html.twig');
}
}
And here's my route:
registro-producto:
path: /ingresar-producto/{id}
name_prefix: nuevoProducto
controller: App\Controller\ProductoController::registro
If I don't send a parameter I don't have any problems but when I want to send an ID it comes the issue, and if I set the route with a parameter and I don't send a parameter I get an error telling me that I must have to send a parameter. I don't know what's going on...
UPDATE
This is what I get when I run php bin/console debug:router
Thank you all!
Upvotes: 0
Views: 464
Reputation: 6568
This is because you haven't declared the param type.
Try amending your routes file to this:
registro-producto:
path: /ingresar-producto/{id}
name_prefix: nuevoProducto
controller: App\Controller\ProductoController::registro
requirements:
id: \d+
Doing this will set the {id}
type to digit, allowing the /3
param.
See here:
Taken from Symfony Docs:
Adding {wildcard} Requirements
Imagine the blog_list route will contain a paginated list of blog posts, with URLs like /blog/2 and /blog/3 for pages 2 and 3. If you change the route's path to /blog/{page}, you'll have a problem:
blog_list: /blog/{page} will match /blog/*;
blog_show: /blog/{slug} will also match /blog/*.
When two routes match the same URL, the first route that's loaded wins. Unfortunately, that means that /blog/yay-routing will match the blog_list. No good!
To fix this, add a requirement that the {page} wildcard can only match numbers (digits)
ref: https://symfony.com/doc/current/routing.html
Upvotes: 1