PHP上的自定义MVC无法在服务器上正常工作

问题描述 投票:-2回答:1

我在php上编写了自定义MVC。这是我的index.php

<?php
    require_once('libs/Bootstrap.php');
    new Bootstrap();
?>

和Bootstrp.php

<?php
class Bootstrap {
public function __construct(){
    //router
    $tokens = explode('/', rtrim($_SERVER['REQUEST_URI'], '/'));

    //dispatcher
    $x = explode('?', rtrim($tokens[1], '?'));
    $controllerName = ucfirst($x[0]);
    if (count($x) > 1)
        $xatrs = explode('=', rtrim($x[1], '='));
    else
        $xatrs = ['-1'];
    if ($controllerName == 'Home') {
        if ($xatrs[0] == 'sstatus') {
            require_once('Controllers/Home.php');
            $methodName = 'index';
            $controller = new Home();
            $controller -> $methodName(['page' => 1, 'order' => 'status', 'dir' => $xatrs[1]]);
        } else if ($xatrs[0] == 'susername') {
            require_once('Controllers/Home.php');
            $methodName = 'index';
            $controller = new Home();
            $controller -> $methodName(['page' => 1, 'order' => 'username', 'dir' => $xatrs[1]]);
        } else if ($xatrs[0] == 'semail') {
            require_once('Controllers/Home.php');
            $methodName = 'index';
            $controller = new Home();
            $controller -> $methodName(['page' => 1, 'order' => 'email', 'dir' => $xatrs[1]]);
        } else if ($xatrs[0] == 'page') {
            error_log("index");
            error_log($xatrs[1]);
            require_once('Controllers/Home.php');
            $methodName = 'index';
            $controller = new Home();
            $controller -> $methodName(['page' => $xatrs[1], 'order' => null]);
        } else if (file_exists('Controllers/'.$controllerName.'.php')) {
            require_once('Controllers/'.$controllerName.'.php');
            $controller = new $controllerName;
            $args = null;
            if (isset($tokens[2]) && $tokens[1] != 'Login') {
                $atr = explode('?', rtrim($tokens[2], '?'));
                $methodName = $atr[0];
                $atrs = explode('&', rtrim($atr[1], '&'));
                $args = array();
                foreach ($atrs as $item) {
                    $vals = explode('=', rtrim($item, '='));
                    $args[urldecode($vals[0])] = urldecode($vals[1]);
                }
            } else {
                $methodName = 'index';
            }

            $controller -> $methodName($args);
        } else {
            $controllerName = 'Errorr';
            require_once('Controllers/'.$controllerName.'.php');
            $controller = new $controllerName;
            $controller -> indexAction();
        }
    } else if ($controllerName == 'Login') {
        require_once('Controllers/'.$controllerName.'.php');
        $controller = new $controllerName;
        error_log("ABRAKDATA");
        error_log($controllerName);
        $args = null;
        if (isset($tokens[2]) && $tokens[2] != 'index') {
            $atr = explode('?', rtrim($tokens[2], '?'));
            $methodName = $atr[0];
            $atrs = explode('&', rtrim($atr[1], '&'));
            $args = array();
            foreach ($atrs as $item) {
                $vals = explode('=', rtrim($item, '='));
                $args[urldecode($vals[0])] = urldecode($vals[1]);
            }
        } else {
            $methodName = 'index';
        }

        $controller -> $methodName($args);
    }
}
}

简而言之,bootstrap.php文件解析URL并向相应的控制器发出请求。我的网址结构localhost / Home?page = 1,这里Home是控制器名称。我通过php -S命令启动了本地服务器,一切正常。但是,当我在服务器上部署它时,服务器只能打开索引页,而当我尝试使localhost / Home?page = 1时,会打开这种请求404页。这是完整项目https://github.com/AbylayOmar/TaskApp.git的链接这是服务器elitech.kz的链接

php url-routing
1个回答
0
投票

问题出在我的.htaccess文件中。我使所有请求都通过index.php

© www.soinside.com 2019 - 2024. All rights reserved.