Community

Do I need to die/exit the script after using sendRedirect()?

Do I need to die/exit my snippet after using sendRedirect()?

That’s all :slight_smile:

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.

1 Like

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.

1 Like

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