concept redirect in category php

appears as: redirect, redirects, redirect
PHP in Action: Objects, Design, Agility

This is an excerpt from Manning's book PHP in Action: Objects, Design, Agility.

  • Some commands do a redirect. There is no simple way to detect the fact that a redirect has been sent, and it’s customary to exit unceremoniously after a redirect. Exiting causes the test script to stop executing before it reports any test results.
  • Now we’ve solved one of the problems that make testing awkward. One remains: the fact that commands sometimes need to issue redirects. Redirects are typically used after successful form submissions. In fact, it’s good practice to redirect after a POST request and then use GET to show the next page.

    It’s hard to test whether the code has done a redirect. If we could return a value instead, testing would be easy. But we want to do different things depending on circumstances: Some commands will return a template so that we can redisplay the form. Others will return something to tell us to do a redirect so that we can display the news list. So if we return something that is not a template (say, a string representing a URL to redirect to), we may need to start adding conditional logic like this:

    if ($template instanceof Template) {
        $template->display();
    }
    else { //The template is actually a URL for redirect?
        header("Location: $template");
        exit;