如何正确地在基于路由的MVC中实现登录/寄存器系统



我启动了一个小项目,其中有一个路由器,它将返回视图,具体取决于您填写的URL(如果存在)。

<?php
require_once 'page.php';
class Route
{
     private $_uri = array();
     private $_method = array();
/*
 * Builds a collection of internal URL's to look for
 * @param type $uri
 */
public function add($uri, $method = null)
{
    $this->_uri[] = '/' . trim($uri, '/');
    if($method != null){
        $this->_method[] = $method;
    }
}
public function submit()
{
    $uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
    $page = new Page('index.twig');
}

}

现在,模板是硬编码,但是我想以后从数据库中获取页面名称,以便我可以拥有动态页面。这就是为什么我需要知道如何使用此代码进行适当的登录系统。我想要"/登录"链接到登录页面,将我重定向到仪表板,在那里我可以管理数据,例如WordPress,创建页面等。如果我错过了数据,请在评论中问我。

如果要创建一个登录系统,那么您很可能需要拥有一组只能由登录int用户访问的URL。

private $_logged_in_uris = [];

接下来,您需要修改add()函数以识别需要认证的URL。

/*
 * Builds a collection of internal URL's to look for
 * @param type $uri
 */
public function add($uri, $method = null,$authenticated=false)
{
    $this->_uri[] = '/' . trim($uri, '/');
    if($authenticated){
       $this->_logged_in_uris[] = '/' . trim($uri,'/');
    }
    if($method != null){
        $this->_method[] = $method;
    }
}

我可以问为什么您使用$ _get ['uri']?最好直接从PHP暴露给您的超级全球群体中获取URI。在这种情况下,$ _server ['request_uri']很方便。

要弄清楚用户是否经过身份验证,一旦用户登录了用户名和密码,就需要设置会话变量。然后,您可以将URI与$ _logged_in_uris中的URI进行比较,然后检查会话变量。如果设置了会话变量,则可以访问您网站的该部分。

最新更新