Lindaia
Lindaia

Reputation: 248

Symfony 4 : Controller requires that you provide a value for the "$id" argument

I have a page with a list of projects. When you click on one project, you should arrive on a "project_detail" page.

Here is my MainController with the route to project_detail :

 /**
 * @Route("/project_detail/{$id}", name="project_detail")
 */
public function project_detail($id)
{
    $current_project = $this->getDoctrine()
        ->getRepository(Project::class)
        ->find($id);

    return $this->render('main/project_detail.html.twig', [
        'current_project' => $current_project,
    ]);
}

And here is my template project_list, with the link to the "project_detail" page :

<a href="{{ path('project_detail', {'id':project.id }) }}">
    <div>...</div>
</a>

I obtain this link by clicking on it: website/project_detail/%7B%24id%7D?id=1

So I suppose that it understands that the page should show the project which have the id number 1. However, it keeps displaying this error : Controller "App\Controller\MainController::project_detail()" requires that you provide a value for the "$id" argument.

Do you have any idea of what I'm doing wrong ?

Thank you for your time,

Upvotes: 2

Views: 1293

Answers (2)

sensorario
sensorario

Reputation: 21698

The variable you pass is "current_project" and this means you need to change:

<a href="{{ path('project_detail', {'id':project.id }) }}">
    <div>...</div>
</a>

to

<a href="{{ path('project_detail', {'id':current_project.id }) }}">
    <div>...</div>
</a>

And remember also to fix this:

/**
 * @Route("/project_detail/{$id}", name="project_detail")
 */

with this

/**
 * @Route("/project_detail/{id}", name="project_detail")
 */

Upvotes: 2

ehymel
ehymel

Reputation: 1391

Change your annotation to this:

/**
 * @Route("/project_detail/{id}", name="project_detail")
 */

The change is to drop the $ in front of the id variable.

Upvotes: 2

Related Questions