我创建了一个名为Router
的类,它导入如下所有控制器:
<?php
include dirname(dirname(__FILE__)) . '\application\controllers\backend.php';
class Router
{
private $_backend;
public function __construct()
{
$this->_backend = new Backend();
}
/**
* Execute function
*/
public function submit($controller, $func)
{
// $this->_backend->index();
}
}
?>
现在这个类可以在我的router.php
文件中使用,这个文件包括在其他任何人之前,我可以通过引用访问任何php文件中的路由器类:
$router = new Router();
我的任务是调用在导入到index
文件中的backend
控制器中可用的函数router.php
。在index.php
文件中,我有:
$router->submit('backend', 'index');
如何匹配控制器名称并调用作为参数传递的函数以及Router
类中的变量?
发布于 2016-02-01 23:44:36
<?php
class Router
{
public function submit($controller, $func)
{
// include dynamically the needed file
include dirname(dirname(__FILE__)) . '\application\controllers\' . $controller . '.php';
// The classname starts with capital
$Class = ucfirst($controller);
// create an instance
$ctr = new $Class();
// and call the requested function
$ctr->$func();
}
}
https://stackoverflow.com/questions/35141278
复制相似问题