如何在mvc中加载默认模型?

时间:2017-03-02 00:54:55

标签: php

我正在写一个MVC项目。

当我的网址为空时,必须加载索引控制器。该部分工作正常,但我还需要它来加载index_model。但无论我尝试过什么,我都无法加载它。

我的Bootstrap代码在这里。如您所见,我已尝试通过

if(empty())部分加载它
$controller->loadModel('index');

但它不起作用。那么该怎么办?请帮帮我。

bootstrap.php中

<?php

class Bootstrap {

    function __construct() {
        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = explode('/', $url);

        if(empty($url[0])) {
            require 'controllers/index.php';
            $controller = new Index();
            $controller->index();
            $controller->loadModel('index');
            return false;
        }

        $file = 'controllers/'.$url[0].'.php';
        if(file_exists($file)) {
            require $file;
        } else {
            $this->cmserror();
        }

        $controller = new $url[0];
        $controller->loadModel($url[0]);

        if(isset($url[2])) {
            if(method_exists($controller, $url[1])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->cmserror();
            }
        } else {
            if(isset($url[1])) {
                if(method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->cmserror();
                }
            } else {
                $controller->index();
            }
        }
    }

    function cmserror() {
        require 'controllers/cmserror.php';
        $controller = new CmsError();
        $controller->index();
        return false;
    }
} 

1 个答案:

答案 0 :(得分:-1)

试试这个? 看看我做的这个: https://github.com/omaraligit/mymvc

class Bootstrap {

    function __construct() {
        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = explode('/', $url);
        //changes are in here 
        $controller = new $url[0];
        $controller->loadModel($url[0]);
        if(empty($url[0])) {
            require 'controllers/index.php';
            $controller = new Index();
            $controller->index();
            $controller->loadModel('index');
            return false;
        }

        $file = 'controllers/'.$url[0].'.php';
        if(file_exists($file)) {
            require $file;
        } else {
            $this->cmserror();
        }



        if(isset($url[2])) {
            if(method_exists($controller, $url[1])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->cmserror();
            }
        } else {
            if(isset($url[1])) {
                if(method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->cmserror();
                }
            } else {
                $controller->index();
            }
        }
    }

    function cmserror() {
        require 'controllers/cmserror.php';
        $controller = new CmsError();
        $controller->index();
        return false;
    }
}