1

I want to access a specific product from a homepage :

<a href="{{ path('property.show', {id: property.id, slug: property.slug}) }}">{{ property.title }}</a>

I specified the route in the controller

    /**
     * @Route("/property/{slug}-{id}", name="property.show", requirements={"slug": "[a-z0-9\-]*"})
     * @param Property $property
     * @return Response
     */
    public function show(Property $property, string $slug): Response
    {
        if ($property->getSlug() !== $slug)
        {
            return $this->redirectToRoute('property.show', [
                'id'   => $property->getId(),
                'slug' => $property->getSlug()

            ], 301);
        }
        $property = $this->repository->find($id);
        return $this->render('property/show.html.twig', [
                'property' => $property,
                'current_menu' => 'properties'
            ]);

but when I click on the link, I get an error "No route found for "GET /my-first-property/property/-1" (from "http://localhost:8000/")"

The URL I'm trying to generate should be /property/my-first-property-1, I don't understand why it doesn't work.

Charlene C
  • 15
  • 5
  • 1
    Are you sure there isn't another controller action with the same route name? The route for this action is `/biens/{slug}-{id}`, it starts with `/biens`, not with `/property`. – Bartosz Zasada Jun 23 '20 at 13:11
  • I wanted to translate everything to English and I updated my code after reviewing my question, I just copied-pasted at the last moment. I edited my first post with the correct words. There is no other controller action with the same route name though. – Charlene C Jun 23 '20 at 13:17
  • I have another function in the controller using this route : ```@Route("/property", name="property.index")``` Can this be the issue ? – Charlene C Jun 23 '20 at 13:22
  • 1
    Your controller action does a redirect to the same route. Have you checked if the wrong path gets generated in twig or later when trying to do the redirect? `dump(path('property.show', {id: property.id, slug: property.slug}))` looks correct? – simon.ro Jun 23 '20 at 13:28
  • @simon.ro I'm not sure where to check what you said. I added this ```dump()``` in the function but I get an error because of the { in front of id. If I do a ```debug:router```, it shows this for the path ```property.show ANY ANY ANY /property/{slug}-{id}``` And ```router:match``` shows ```C:\xampp\htdocs\MaSuperAgence>php bin/console router:match /property/my-first-property-1``` [OK] Route "property.show" matches``` – Charlene C Jun 23 '20 at 13:39
  • 1
    @CharleneC thats a twig function, you should add the dump in the template somewhere between {{ }} – simon.ro Jun 23 '20 at 14:14
  • @simon.ro I didn't see your answer, I removed the redirection and added the dump in the template and I see the wrong path is generetad. It looks like that ```/property/-1```. so the slug is not generated but I don't understand why it appears before. – Charlene C Jun 24 '20 at 08:35
  • 1
    If you print `{{ property.slug }}` do you see it correctly? If not, there's your problem. – Martin M. Jun 24 '20 at 09:23
  • @MartinM. I see it correctly : "my-first-property". I tried to route it with yaml but the URL still appears as "{slug}/property/-{id}". – Charlene C Jun 24 '20 at 09:44
  • 1
    Do you really need your route to be `/property/{slug}-{id}`? If you take a more standard approach and make it `/property/{id}/{slug}` it should work. – Martin M. Jun 24 '20 at 11:23
  • @MartinM. I tried changing the route as you said and the slug was still first ```{slug}/{property}/{id}```. So I tried without the ```/property``` and I noticed it always generates a ```/``` because it became ```/{slug}//{id}```. – Charlene C Jun 24 '20 at 17:52
  • 1
    @CharleneC did you also add `{property}` when you generate the route in twig? Slug is usually meant to be last. Check [this](https://symfony.com/doc/current/routing.html#optional-parameters) – Martin M. Jun 25 '20 at 05:01
  • @MartinM. I removed the slug, the problem comes from there. I'm not sure why because there was no issues when I printed it. I will find another way to generate it then.Thank you for your time ! – Charlene C Jun 25 '20 at 08:07

0 Answers0