Php 如何重写路由器/模板类集,使其不会';不要破坏内部函数?

Php 如何重写路由器/模板类集,使其不会';不要破坏内部函数?,php,model-view-controller,Php,Model View Controller,因此,我最近学习了两个模型-视图-控制器教程,并提出了一个工作系统。然而,它显示了一些有趣的问题。当我需要在控制器中调用某个操作/函数时,它会调用它,但我无法成功地离开它。可以使用header强制执行,但这可能不是一个好的解决方案 主要类别是路由器: <?php class router { /* * @the registry */ private $registry; /* * @the controller path */ private $path; private $args

因此,我最近学习了两个模型-视图-控制器教程,并提出了一个工作系统。然而,它显示了一些有趣的问题。当我需要在控制器中调用某个操作/函数时,它会调用它,但我无法成功地离开它。可以使用header强制执行,但这可能不是一个好的解决方案

主要类别是路由器:

<?php

class router {
/*
* @the registry
*/
private $registry;
/*
* @the controller path
*/
private $path;
private $args = array();
public $file;
public $controller;
public $action;

function __construct($registry) {
    $this->registry = $registry;
}

function setPath($path) {

        /*** check if path i sa directory ***/
        if (is_dir($path) == false)
        {
            throw new Exception ('Invalid controller path: `' . $path . '`');
        }
        /*** set the path ***/
        $this->path = $path;
}

public function loader()
{
    /*** check the route ***/
    $this->getController();



    /*** if the file is not there diaf ***/
    if (is_readable($this->file) == false)
    {
        // TODO ? //
        // Route and Display a 404 Error Page when system is ready //
        echo "404: Not Found";
    } else {

        /*** include the controller ***/
        include $this->file;

        /*** a new controller class instance ***/
        $class = $this->controller . 'Controller';
        $controller = new $class($this->registry);

        /*** check if the action is callable ***/
        if (is_callable(array($controller, $this->action)) == false)
        {
                $action = 'news';
        }
        else
        {
                $action = $this->action;
        }
        /*** run the action ***/
        $controller->$action();
    }
}

private function getController() {

    /*** get the route from the url ***/
    $route = (empty($_GET['task'])) ? '' : $_GET['task'];

    if (empty($route))
    {
        $route = 'news';
    }
    else
    {
        /*** get the parts of the route ***/
        $parts = explode('/', $route);
        $this->controller = $parts[0];
        if(isset( $parts[1]))
        {
            $this->action = $parts[1];
        }
    }

    if (empty($this->controller))
    {
        $this->controller = 'news';
    }

    /*** Get action ***/
    if (empty($this->action))
    {
        $this->action = 'index';
    }

    /*** set the file path ***/
    $this->file = $this->path .'/'. $this->controller . 'Controller.php';
   }         
}

一旦我进入了函数,我就不能再使用模板重定向了——它只是简单地中断了

    <?php

    class template {

    /*
     * @the registry
     * @access private
     */
    private $registry;

    /*
     * @Variables array
     * @access private
     */
    private $vars = array();

    /**
     *
     * @constructor
     *
     * @access public
     *
     * @return void
     *
     */
        function __construct($registry) {
            $this->registry = $registry;

        }
         /**
         *
         * @set undefined vars
         *
         * @param string $index
         *
         * @param mixed $value
         *
         * @return void
         *
         */
        public function __set($index, $value)
        {
            $this->vars[$index] = $value;
        }

        public function show($name) {

            $path = SITE_PATH . '/system/views/' . $name . '.php';

            if (file_exists($path) == false)
            {
                throw new Exception('Template not found in '. $path);
                return false;
            }

            // Load variables
            foreach ($this->vars as $key => $value)
            {
                $key = $value;
            }

            include($path);  

        }
    }

    ?>


我注意到它似乎试图将新位置添加到现有的URL上,例如user/login/news,这时它应该重定向到/news。我能做些什么来修复此问题?

已修复。我想Pastebin会更实用,因为这些都是完整的类。我认为Pastebin会更实用,因为这些都是完整的类。