Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection PHP inside MVC

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.

like image 892
aephixus Avatar asked Apr 25 '26 17:04

aephixus


2 Answers

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
    */
}
like image 139
RobertPitt Avatar answered Apr 27 '26 10:04

RobertPitt


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.

like image 31
Wige Avatar answered Apr 27 '26 11:04

Wige



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!