3

I am developing a personal framework purely on PHP.

Let's say I am in a method inside a controller and I want to redirect to another page how would I do that? At least conceptually.

3
  • php.net/manual/en/function.header.php Commented Feb 12, 2011 at 17:57
  • 1
    Your question has nothing to do with MVC. Commented Feb 12, 2011 at 18:07
  • 3
    mario, you didn't understand his question. Just look at the answer below. Commented Feb 18, 2012 at 4:06

2 Answers 2

11

If your developing MVC You should have an input class and an output class (I/O), you should create a function called redirect within the output class and build the new url from your base url like so:

public function redirect($controller,$method = "index",$args = array())
{
    global $core; /* Guess Obviously */

    $location = $core->config->base_url . "/" . $controller . "/" . $method . "/" . implode("/",$args);

    /*
        * Use @header to redirect the page:
    */
    header("Location: " . $location);
    exit;
}

This way within your controller you can simply use the input class do your redirect for you.

class MyController extends BaseController
{
    public function login()
    {
        if($this->library->session->exists("user_logged_in") === false)
        {
            $this->library->output->redirect("MyController","login",array("from:login"));
        }
    }
    /*
       ..More Here
    */
}
Sign up to request clarification or add additional context in comments.

Comments

2
header("Location: http://domain.com/folder/page.html", 301);
exit();

This code must be the first output of the script. You can not perform redirection after generating any output to the client. Once you have sent the redirection to the client, you can exit the script because any additional output generated would not be seen by the user.

1 Comment

+1. i've never understood why people offer answers in the comments section, but thanks for rectifying the situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.