Goodbye, Drupal_Goto Command... So How do I Redirect Users?

Drupal_Goto() is dead, long live Drupal_Goto()! Trying to forward your user to a new page with PHP from within Drupal?

There's a years-long discussion on Drupal.org about this, and why we couldn't have our wonderful, precious, easy-like-BASIC-to-remember command to send users on their way, which culminated in statements like this one from "SiliconMind"
To quote "Silicon Mind" from Drupal.org:


This is great yet sad example of why Perfect is the enemy of good. In D7 the only thing that was needed in order to alter a redirection, was to implement a hook. A three liner solution. In D8 two files are needed and a bunch of "use" calls that take more lines than the actual implementation itself :( Who the hell will memorize this?

So, you're probably not here reading this because you want to know how to redirect a form, or because you're writing a module. I suspect you're banging out a script and just want to move your user down the stream without a lot of rigamarole. I get it.

So.... here's how you do it:

***TRANSMISSION INTERRUPTED***



OK, I'm just kidding. But, you're not going to like this. You see, this issue has been a real roller-coaster ride through Drupal8 develpment. And it seems the only... yes, ONLY way to do this (at least now as of Release Candidate 4) is to print out a META tag to have the client browser redirect itself after one or zero seconds. I know, it sucks.

print '<meta http-equiv="refresh" content="0;URL=\'http://example.com/\'" />';



Whelp, there it is. Print that out and your user will continue on to their destination. But it's no Drupal_Goto('http://BritneySpears.com') that's for sure!

I know you don't believe me, and you're going to continue searching. Bon voyage! I wish you luck in your travels.

Tags: