0

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?

2 Answers 2

2

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');
0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.