具有PHP且没有框架的基本MVC

问题描述 投票:0回答:1

您好,我正在尝试开展教育活动,我必须开发没有框架的应用程序才能理解MVC的概念,这是我的结构enter image description here

index.php

<?php

require_once("router.php");
$route = new Router();
if (isset($_GET['controller']) && isset($_GET['action'])) {
    $controller=$_GET['controller'];
    $accion = $_GET['action'];
    $route->run($controller,$accion);
} else {
    $route->run();
}

router.php

<?php
    class Router{

        public function run($controller='index',$action='index'){
            require_once("config/db.php");
            if($controller=='index' && $action=='index'){
                require_once("controllers/index_controller.php");
                $controller = new IndexController();
                $controller->index();
            }

            else if($controller=='people' && $action=='index'){
                require_once("controllers/people_controller.php");
                $controller = new PeopleController();
                $controller->index();
            }   

        }   
    }

问题出在我拥有此.htaccess文件的友好网址中:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^index/index$ index.php?controller=index&action=index [QSA]
RewriteRule ^people/index$ index.php?controller=people&action=index [QSA]

但是这里写的规则不起作用。

php .htaccess url model-view-controller
1个回答
0
投票

我使用了您的结构,并开发了一个有效的示例:

。htaccess

#redirect when not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect ex. people/ -> index.php?controller=people
RewriteRule ^([^/]+)/?$ index.php?controller=$1 [NC,L,QSA]
# redirect ex. people/index/ -> index.php?controller=people&action=index
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?controller=$1&action=$2 [NC,L,QSA]

index.php

<?php

require "router.php";
require "config/db.php"; /** moved this at top-level **/

/** todo: move this into a config file, then include it in index **/
function getValidRequest($get_key, $default_value) {
    /** put here validation methods for get paramenters **/
    $get_value = filter_input(INPUT_GET, $get_key, FILTER_SANITIZE_ENCODED);

    if (is_null($get_value))
        $get_value = $default_value;

    return $get_value;
}

$route = new Router();

$route->run(
    getValidRequest("controller", "index"),
    getValidRequest("action", "index")
);

?>

router.php

<?php

class Router {

    private $controller;

    private function _loadControllerFile($controller) {
        /* convert: "index" to "controller/index_controller.php" */
        $controller_file = "controllers/{$controller}_controller.php";
        try {
            if (!file_exists($controller_file))
                throw new Exception ('Router Error: unknown controller: ' .$controller);
            require $controller_file;
        } catch (Exception $e) {
            die( $e->getMessage() );
        }
    }

    private function _loadControllerClass($controller) {
        /* convert: "index" to "IndexController" */
        $class_name = ucfirst($controller)."Controller";
        try {
            if (!class_exists($class_name))
                throw new Exception ('Router Error: missing controller class: ' .$class_name);
            $reflection = new ReflectionClass($class_name);
            $this->controller = $reflection->newInstanceArgs();
        } catch (Exception $e) {
            die( $e->getMessage() );
        }
    }

    private function _loadControllerAction($action) {
        /* convert: "index" to "(class)->index()" */
        try {
            if (!method_exists($this->controller, $action))
                throw new Exception ('Router Error: missing method in controller class: ' .$action);
            $this->controller->{$action}();
        } catch (Exception $e) {
            die( $e->getMessage() );
        }
    }

    public function run($controller, $action) {

        $this->_loadControllerFile($controller);

        $this->_loadControllerClass($controller);

        $this->_loadControllerAction($action);

    }   
}

?>

它在我的末端起作用。我会改用经过良好测试的框架,但这是一个很好的练习。 (如果需要,您可以只使用.htacces文件,但我建议您也查看对Router类和索引建议的更改)。

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