如何重写路由器/模板类集,以便它不会破坏函数内部?

时间:2015-07-28 17:41:17

标签: php model-view-controller

所以我最近关注了几个模型视图控制器教程,并提出了一个工作系统。然而,它表现出一些有趣的问题。当我需要在控制器中调用一个动作/功能时,它会调用它,但我无法成功地远离它。它可以强制使用标题,但这可能不是一个好的解决方案。

主要类是路由器:

<?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';
   }         
}

&GT;

一旦我进入该功能,我就不能再使用该模板进行重定向 - 它只会中断。

    <?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);  

        }
    }

    ?>

我注意到它似乎正在尝试将新位置添加到现有网址上,例如用户/登录/新闻 - 何时应该重定向到/ news。我该怎么做才能解决这个问题?

0 个答案:

没有答案