I'm having trouble doing a redirect to a named route. Here is my routing.yml and controller code:

bncreports.user_doc_upload:
  path: "/user-doc-upload"
  defaults:
    _controller: '\Drupal\bncreports\Controller\FileController::userDocUpload'
    _title: "User Documents"
  requirements:
    _role: "administrator+bnc_operator"

...
bncreports.delete_file:
  path: "/delete-file"
  defaults:
    _controller: '\Drupal\bncreports\Controller\FileController::delete'
  requirements:
    _role: "administrator+bnc_operator"
...
class FileController extends ControllerBase
{
...

public function delete(Request $request): Response
  {
    $path = trim(urldecode($request->query->get('path')));
    $route = trim(urldecode($request->query->get('route')));

    if (file_exists($path)) {
      unlink($path);
      \Drupal::messenger()->addMessage("$path has been deleted");
    } else
      \Drupal::messenger()->addMessage("Cannot find $path", 'error');

    return $this->redirect($route);
  }

...
}
{{ form }}

<br><br>
<p><strong>{{ path }}</strong></p>

<table>
	<tr>
		<th>Name</th>
		<th>Size</th>
		<th>Modified</th>
		<th></th>
	</tr>
  {% for item in content %}
		{% if (item != "." and item != "..") %}  
		<tr>
			<td><a href="/download-file?path={{ path|url_encode }}{{ "/"|url_encode }}{{ item|url_encode }}">{{ item }}</a></td>
			<td>{{ filesize("#{path}/#{item}") }}</td>
			<td>{{ filemtime("#{path}/#{item}")|date("m/d/Y h:ia") }}</td>
			<td><a href="/delete-file?path={{ path|url_encode }}{{ "/"|url_encode }}{{ item|url_encode }}&route=bncreports.user_doc_upload">delete</a></td>
		</tr>
	  {% endif %}
  {% endfor %}
</table>

I get a Page Not Found and the browser's url has xxxxxx/bncreports.user_doc_upload

I'm confused. Why is the route name in the url? This should work. The path in bncreports.user_doc_upload which is /user-doc-upload works  fine.

Comments

jaypan’s picture

You'll have to show us how you're performing the redirect. You also have showed us a ::delete() method, but did mot indicate what class it was, if it was your controller, or what, so it's not enough information.

Contact me to contract me for D7 -> D10/11 migrations.

donpwinston’s picture

I've added the class FileController to the code above.

The redirect occurs with:

return $this->redirect('<current>');

<current> is 'bncreports.user_doc_upload'. I use the Devel module. It lists this route so it's there and the path works.

Simpler is always better.

jaypan’s picture

You haven't shown how you are calling

Drupal\bncreports\Controller\FileController::delete

So, there is still some missing information. But you can try switching <current> with the actual route name

bncreports.user_doc_upload

Contact me to contract me for D7 -> D10/11 migrations.

donpwinston’s picture

I've added the yml code for the /delete-file path that calls the FileController::delete method above.

In Devel the '<current>' value is 'bncreports.user_doc_upload'. I've put the string 'bncreports.user_doc_upload' in place of '<current>' and I get the same result. It appears that something is stuck but I've cleared the cache numerous times.

Simpler is always better.

donpwinston’s picture

I got it to work. My twig template was stuck and needed to be refreshed. I also added a destination route to the calling url instead of using <current>. It works as I expect now. Thank you.

Simpler is always better.