Hi all,
in a hook, I have to do a simple redirect to "/node/1" (the view of a node) at the end of the function.

If I use:
return new RedirectResponse(Url::fromInternalUri($newUrl));
I have this error:
"Error: Call to protected method Drupal\\Core\\Url::fromInternalUri() from context 'Drupal\\myapi\\Controller\\RestApiController' in /var/www/mysite/web/modules/custom/mysite/myapi/src/Controller/RestApiController.php on line 109 #0"

How can I do this simple redirect?
I'm using Drupal 8.

Thank you very much.



slewazimuth’s picture

Url::fromInternalUri is listed as a static protected function. If you insist on passing functions as arguments, choosing the appropriate public function would be better suited.

ilclaudio’s picture

I've tried:
$url = Url::fromRoute('entity.node.canonical', array('node' => $nodeId));
return new RedirectResponse($url->toString());

But it does not redirect to the node.
I miss drupal_goto() so much!!


ilclaudio’s picture

I've solved in this way:

$routeName = 'entity.node.canonical';
$routeParameters = ['node' => $nodeId];
$url = \Drupal::url($routeName, $routeParameters);
return new RedirectResponse($url);