PHP MVC - 在__construct中返回call_user_func_array()

时间:2017-01-09 20:44:50

标签: php

我正在开发一个MVC网站,但我遇到了问题。我无法从控制器返回任何内容。我可以执行$this->someMethod()但不能return "hello"这样的事情我认为它与call_user_func_array()有关所以我该如何解决这个问题?

我的代码:

路由器:

Class App
{

protected $controller = 'home';
protected $method = 'index';
protected $parameters = [];

public function __construct()
{
    $url = $this->Url();

    if(file_exists('../app/controllers/'. $url[0] .'.php')){

        $this->controller = $url[0];
        unset($url[0]);
    }

    require_once '../app/controllers/'. $this->controller .'.php';

    $this->controller = new $this->controller;

    if (isset($url[1])){
        if (method_exists($this->controller, $url[1])){
            $this->method = $url[1];
            unset($url[1]);
        }
    }

    $this->parameters = $url ? array_values($url) : [''];

    call_user_func_array([$this->controller, $this->method], $this->parameters);

}

public function Url()
{
    if(isset($_GET['url'])){
        $url = filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL);
        $exploded_url = explode('/', $url);
        return $exploded_url;
    }
}
}

Public Index.php:

<?php

    require_once '../app/core/init.php';

    $app = new App;

控制器:

class Home extends Controller
{
public function index($name)
{
    //this works.
    $this->view('index');

    //this does not work.

    return "hello";

    }
}

那我该怎么解决呢,所以return也有效?

0 个答案:

没有答案