繁体   English   中英

重定向过多,PHP MVC

[英]Too many redirects PHP MVC

class Core {

protected $currentController = '';
protected $currentMethod = '';
protected $params = [];

public function __construct() {

    $url = $this->getUrl();

    $pages = [
        "" => ["controller" => "Pages", "method" => "index"],
        "profile" => ["controller" => "Pages", "method" => "profile"],
        "help" => ["controller" => "Pages", "method" => "help"],
        "signin" => ["controller" => "Pages", "method" => "signin"]
    ];

    // cant access controller
    $noaccess = ["pages"];


    if (in_array($url[0], $noaccess)) {
        redirect("/");
    }


    if (isLoggedIn()) {
        if (!in_array($url[0], $noaccess)) {
            if (!array_key_exists($url[0], $pages)) {
                if (file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
                    // If exists, set as controller
                    $this->currentController = ucwords($url[0]);
                    $this->currentMethod = "index";
                    // Unset 0 Index
                    unset($url[0]);
                } else {
                    // 404
                    $this->currentController = "Pages";
                    $this->currentMethod = "error404";
                    unset($url[0]);
                }
            } else {
                foreach ($pages as $page => $options) {
                    if ($url[0] == $page) {
                        $this->currentController = $options['controller'];
                        $this->currentMethod = $options['method'];
                        //unset($url[0]);
                    }
                }
            }
        }
    } else {
        redirect("signin");
    }


    // Require the controller
    require_once '../app/controllers/' . $this->currentController . '.php';

    // Instantiate controller class
    $this->currentController = new $this->currentController;

    // Check for second part of url
    if (isset($url[1])) {
        // Check to see if method exists in controller
        if (method_exists($this->currentController, $url[1])) {
            $this->currentMethod = $url[1];
            // Unset 1 index
            unset($url[1]);
        }
    }

    // Get params
    $this->params = $url ? array_values($url) : [];
    // Call a callback with array of params
    call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}

public function getUrl() {
    if (isset($_GET['url'])) {
        $url = rtrim($_GET['url'], '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);
        return $url;
    }
}

}

我正在学习如何为PHP创建自己的MVC框架。 我正在尝试在基于URL实例化控制器的核心类中重定向用户。

example.com/posts/将实例化Post Controller。

如果他们未登录,我想将其重定向到/signin/如果用户未登录,则无法访问任何页面。

我有一个称为isLoggedIn()的基本函数,该函数检查$_SESSION变量。 我能够测试它是否可以与die()命令一起使用。

一切正常,但是出现错误,提示重定向过多。 我有关$noaccess重定向在没有此问题的情况下有效,但是我无法使loginIn正常工作。 我不确定为什么会有这个问题。

感谢Zeke: https : //stackoverflow.com/users/3654197/zeke

每当我重定向到/ signin /时,它也在尝试重定向,这导致了循环。

我设置了一个条件,仅在网址不是/ signin /时才重定向

  if ( !isLoggedIn()) {
    if ($url[0] == "signin") {
      $this->currentController = "Pages";
      $this->currentMethod = "signin";
    } else {
      redirect('signin');

    }
    unset($url[0]);
  }

如果每个页面(包括登录页面)都创建一个新的Core对象,那么每当您尝试登录(访问登录页面)时,它都会以无休止的循环不断重定向到自身。

添加条件或仅在构造函数中不检查登录数据即可解决问题。 有很多方法可以做到这一点,但这是一般的想法。

最后,所有页面都运行相同的逻辑,但登录页面除外,后者将其反转。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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