Symfony 4 Route gets mismatched - redirectToRoute leads to the current page

310 views Asked by At

Dear intelligent hive,

I got problems with redirect in symfony 4. Of course I tried to find a similar question, but found no solution.

I just started learning Symfony 4 by working through some tutorials. (AND I LOVE IT!!!)

The program is a really simple blog example on a local apache server (maybe this is important). Means List, Add, Show

So my problem is in the add method. After adding an article (which works fine), I want to use redirectToRout('article_list'). But that doesn't lead me anywhere except on the add-page.

When I debug my routes, I can see the correct routes:

article_list               ANY      ANY      ANY    /article                          
article_delete             DELETE   ANY      ANY    /article/delete/{id}              
article_add                ANY      ANY      ANY    /article/add                      
article_show               ANY      ANY      ANY    /article/{id}

And these routes all work fine, when I type them in manually.

The add-function is also pretty simple:

/**
     * @Route(
     * "/article/add", 
     * name="article_add",
     * )
     */
    public function add(Request $request)
    {
        $article = new Article();
        $form = $this->createFormBuilder($article)
            [...]
        ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()){
            $article = $form->getData();

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($article);

            $entityManager->flush();

            $this->redirectToRoute('article_list');
        }


        return $this->render('article/new.html.twig', array(
            'form' => $form->createView()
        ));
    }

As well as the list-function

/**
     * @Route(
     * "/article", 
     * name="article_list",
     * )
     */
    public function index()
    {
        $articles = $this->getDoctrine()->getRepository(Article::class)->findAll();
        return $this->render('article/list.html.twig', [
            'articles' => $articles,
        ]);
    }

As you can see, the name in the annotations is correct.

When I change the redirectToRoute-Method to

redirectToRoute("/article");

I get the error "Unable to generate a URL for the named route "/article" as such route does not exist. In the log, I see Matched route "article_add" So the route "/article" was matched to the route "article_add". Thats not, what I would expect....

So I kind of have the feeling, that something on the add-page is ignoring my routes.

Does anybody have any idea, what simple problem I'm not able to see?

Thanks! I would really like to understand, whats going wrong here...

1

There are 1 answers

1
El_Vanja On BEST ANSWER

You're not returning the redirection. Do this:

return $this->redirectToRoute('article_list');

That method only creates the response and you need to explicitly return.