创建自己的PHP框架,如何将参数发送到页面控制器


Creating own PHP framework, How to sending parameters to page controller

我正在创建自己的框架。它是这样工作的localhost/controller/action/firstVariable/second/third(依此类推…)我的引导程序如下:

$request        = Util::getInput('request');
$requestList    = explode("/",$request);
$modelName      = @ucwords($requestList[0]);
$action         = @$requestList[1];
$parameters = array_slice($requestList,2);
$controllerName = $modelName.'s'.'Controller';

我从url中获取参数,并将它们保存在变量$parameters中。我想把它们发送到我的控制器中的当前操作,就像Laravel 5正在做的那样。

例如,在Laravel中,我在url中指定参数,就这样。

要给他们打电话,我需要做一个简单的步骤。只需定义它们:

public function firstAction($first,$second){
}

当我转到如下网址时:localhost/Main/firstAction/first/second/

操作"firstAction"的函数将捕获这两个参数,然后基本上我可以在控制器内部调用它们并将其发送到视图。

我的扩展控制器类:

class Controller{
public function __construct($model,$action){
    $modelClass = new main();
    $reflection = new ReflectionClass($model.'sController');
    $reflection->hasMethod($action) ? $this->$action() : die ('Base Controller call error: Method '. $action .' does not exist in Controller '. $model.'sController');
}
public static function renderView($action,$model,$data){
    $model = str_replace('sController','',$model);
    //include '../application/views/'.$model.'/'.$action.'.php';
    $loader = new Twig_Loader_Filesystem('../application/views/'.$model);
    $twig = new Twig_Environment($loader);
    echo $twig->render($action.'.php', $data);
}

}

class MainsController extends Controller {
private $_data = array();
public function __construct($model,$action){
    parent::__construct($model,$action);
}
public function firstAction($first,$second){
    echo 'Hoi';
}

}

我该怎么做,好方法?我当然可以将变量$参数发送到MainController,然后调用$this->_data在我的操作中,但它并不有效。我想我需要使用数组来实现,但我不知道如何实现。

谢谢。

退房http://php.net/manual/en/function.call-user-func-array.php

p.S。您不必使用反射来检查该对象实例上是否存在方法。单个函数调用就足够了。退房http://php.net/manual/en/function.is-callable.php

如果你能使用更多描述性的名字,那就太好了。现在它们令人困惑。