Score:0

Redirecting to a route name

il flag

I've recently built a module. In the twig template that I render, you can delete an item. For example it goes to /delete/item/1. That works fine. that route triggers a delete method from my controller

public function deleteFittest(){
    $connection = \Drupal::service('database');
    $query = $connection->delete('user_fittests')
    ->condition('id', \Drupal::request()->query->get('id'))
    ->condition('user_id', \Drupal::currentUser()->id())
    ->execute();
    return $this->redirect_to('mib_my_fit_test.my_fittest');
}

After the delete, I want to redirect the user to the page that he was on, in this case the 'index' page. But none of the found redirect methods on the internet that I've found work. So what is the proper way to redirect to go to the routing name from my code example?

Score:2
cn flag

I'm not sure what redirect_to is, that's not a method provided by Drupal or Symfony.

The standard way to redirect from a controller, i.e. one that extends Drupal\Core\Controller\ControllerBase, is:

return $this->redirect('route.name');
Score:0
pe flag

It seems you ask to redirect from a controller, but in case you need to redirect from a form :

If you want to redirect from a form embedded in a renderable array of a controller, you can use

$form_state->setRedirect('route.name');
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.