简体   繁体   中英

redirecting URLs in PHP with MVC pattern

I followed MVC pattern to create a web-app. But I got problems redirecting URLs, the format is www.something.com/controller/view/params. Then when the URL is pointing at www.something.com/home and I logout the status will be -> www.something.com/home/logout ( Home controller -> logout method ). But the problem is here, now if I login again the URL is: www.something.com/home/home/login.

I dont know if i should modify the header, the "URL constructor"...

The "url constructor" is:

class App{

protected $controller = 'home';
protected $method = 'index';
protected $params=[];


public function __construct(){



    $url= $this->parseUrl();


    //check if controller exists


    if(file_exists('../app/controllers/' . $url[0] . '.php'))
    {
            $this->controller = $url[0];
            unset($url[0]);
    }
    require_once '../app/controllers/'. $this->controller . '.php';

    $this->controller = new $this->controller;

    if(isset($url[1])){

        if(method_exists($this->controller,$url[1])){




                $this->method = $url[1];
                unset($url[1]);
        }
        else

            echo "that method doesnt exist";


    }

    $this->params =  $url ? array_values($url) : [];

    call_user_func_array([$this->controller,  $this->method],$this->params);
 }


public function parseUrl(){


    if(isset($_GET['url'])){


        return $url = explode('/',filter_var(rtrim($_GET['url'],'/'), FILTER_SANITIZE_URL));




    }




}

}

And the method logout:

 public function logout() {



    session_start();


    $_SESSION = array();


    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]
        );
    }


    session_destroy();


    $this->view('home/login');
}

}

How can I redirect properly? I am doing something wrong? Thanks a lot.

Try to replace the last line of your logout function with this

$this->view('../home/login');

Let me know if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM