我正在做一个小的PHP MVC框架,这是一个有趣的东西。我使用前端控制器index.php来路由所有流量,并根据请求调用新的控制器。然而,我需要一种方法来创建一个仅基于控制器名称(基本上是文件名,即controllers/posts.php)的用户生成控制器的实例。
有没有办法做到这一点?
发布于 2012-03-29 07:52:21
有几种方法可以做到这一点。一种方法是建立一个约定,以便类的名称基于文件的名称。另一种方法是解析文件中的类名。你想看一些小的代码示例吗?
编辑:简单示例
假设我们在某个核心MVC控制器中,我们想要加载一个由第三方用户提供的控制器。你需要做两件事:
使用类definition
加载文件
假设您有一个约定,比如Zend,其中类名映射到文件系统,因此控制器可能是
MyProject/Controller/Login.php
登录控制器的路径,相对于项目的根。按照Zend约定,类的名称应该是MyProject_Controller_Login。如果你想实例化这个类,你需要做的就是:
// load class
require_once 'MyProject/Controller/Login.php';
// instantiate class
$oUserController = new MyProject_Controller_Login();如果需要基于运行时数据实例化类,可以使用保存类名称的变量来实现,因此
$sUserController = 'MyProject_Controller_Login';
$oUserController = new $sUserController();希望这足以让你上手;如果你需要更多,请告诉我。
发布于 2012-03-29 07:57:19
是的,在你的前端控制器或路由器类中检查控制器文件存在后,你可以调用它:这是我从我的路由器类中提取的几个方法,可能对你有帮助,这是一个例子,但如果你遵循,你可以看到一个类如何被加载,一个方法或动作可以被调用:
<?php
/**
* Load Class based on controller or action
*/
public function load_controller(){
/*Get the route*/
$this->getController();
/*Assign front controller to handle routes that dont have core controller
eg ./core/controllers/($this->file.Controller).php
*/
if (is_readable($this->file) === false){
$this->file = $this->path.'/frontController.php';
$this->subaction = $this->action;
$this->action = $this->controller;
$this->controller = 'front';
}
/*Include core controller file*/
include($this->file);
/*Create controllers class instance & inject registry*/
$className = $this->controller.'Controller';
$controller = new $className($this->registry);
/*Check the action method is callable within the class*/
if (is_callable(array($controller, $this->action)) === false){
//index() method because not found method
$action = 'index';
}else{
//action() method is callable
$action = $this->action;
}
/*Run the action method*/
$controller->$action();
}
private function getController() {
$route = (!isset($_GET['route']))?'':$this->registry->function->cleanURL($_GET['route']);
/*Split the parts of the route*/
$parts = explode('/', $route);
$this->request = $route;
$corefolders=array('core','templates');
if (empty($route) || in_array($parts[0],$corefolders)){
$route = 'index';
}else{
//Assign which controller class
$this->controller = $parts[0];
if(isset($parts[1])){
/* Site.com/action */
$this->action = $parts[1];
}
if(isset($parts[2])){
/* Site.com/action/subaction */
$this->subaction = $parts[2];
}
}
/*Set controller*/
if (empty($this->controller)){$this->controller = 'index';}
/*Set action*/
if (empty($this->action)){$this->action = 'index';}
/*Set the file path*/
$this->file = $this->path.'/'.$this->controller.'Controller.php';
}
?> https://stackoverflow.com/questions/9917257
复制相似问题