Do I need to die/exit my snippet after using sendRedirect()?
That’s all
Do I need to die/exit my snippet after using sendRedirect()?
That’s all
No, just be sure there’s a return;
at the end of the snippet. Once MODX hits the sendRedirect()
, a new request will be made.
MODX already calls exit() in modResponse::sendRedirect()
(which modX::sendRedirect calls), so nothing you do after it, will be executed. Not even a return
.
so nothing you do after it, will be executed. Not even a
return
.
Unless the sendRedirect()
is inside an if
statement, which it often is.
I’m sure you have this right, but just to illustrate that with an example for people who aren’t as comfortable with PHP:
if ($shouldRedirect) {
$modx->sendRedirect('foo/bar');
return 'redirecting you'; // <-- this one is *not* executed and can be disregarded
}
// if $shouldRedirect is false, any code outside the if *will* get executed
return 'something else';
Thanks for the replies Bob and Mark!