简体   繁体   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;
    }
}

} }

I'm learning how to make my own MVC framework for PHP. 我正在学习如何为PHP创建自己的MVC框架。 I'm trying to redirect the user in the core class that instantiates a controller based on the url. 我正在尝试在基于URL实例化控制器的核心类中重定向用户。

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

I want to redirect them to /signin/ if they're not logged in. No page will be accessible if the user isn't signed in. 如果他们未登录,我想将其重定向到/signin/如果用户未登录,则无法访问任何页面。

I have a basic function called isLoggedIn() which checks for a $_SESSION variable. 我有一个称为isLoggedIn()的基本函数,该函数检查$_SESSION变量。 I'm able to test if it works with a die() command. 我能够测试它是否可以与die()命令一起使用。

Everything works, but I get an error saying too many redirects. 一切正常,但是出现错误,提示重定向过多。 My redirect regarding the $noaccess works without this issue, but I can't get the loggedIn one to work. 我有关$noaccess重定向在没有此问题的情况下有效,但是我无法使loginIn正常工作。 I'm not sure why it's having this issue. 我不确定为什么会有这个问题。

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

Whenever I was redirecting to /signin/, that was also trying to redirect which caused a loop. 每当我重定向到/ signin /时,它也在尝试重定向,这导致了循环。

I put a condition to only redirect if the the url wasn't /signin/ 我设置了一个条件,仅在网址不是/ signin /时才重定向

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

    }
    unset($url[0]);
  }

If every page creates a new Core object, including the sign in page, then whenever you try to sign in (access the sign in page), it will keep redirecting to itself in a never-ending loop. 如果每个页面(包括登录页面)都创建一个新的Core对象,那么每当您尝试登录(访问登录页面)时,它都会以无休止的循环不断重定向到自身。

Adding a condition or simply not checking for login data in the constructor should do the trick. 添加条件或仅在构造函数中不检查登录数据即可解决问题。 There are many ways to do it, but that's the general idea. 有很多方法可以做到这一点,但这是一般的想法。

In the end, all pages run the same logic except the login page, which reverses it. 最后,所有页面都运行相同的逻辑,但登录页面除外,后者将其反转。

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

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