开发者

Redirection PHP inside MVC

开发者 https://www.devze.com 2023-02-10 16:21 出处:网络
I am developing a personal framework purely on PHP. Let\'s 开发者_JS百科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.If y

I am developing a personal framework purely on PHP.

Let's 开发者_JS百科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.


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
    */
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消