好的,所以我一直在寻找为特定市场开发自己的自定义CMS系统,我一直在寻找一些框架,我遇到的问题是它们没有自动化路由。在laravel中,如果我记得正确回答这样的网址,你会做这样的事情:
Route::get('/user', function()
{
return "This is a user";
});
这基本上会听取特定的请求。现在我想简化这个是创建一个自动路由器。所以我所做的是设置一个.htaccess文件,它接受每个请求并将其定向到index.php。它也会在.com之后执行任何操作,因此类似于www.testsite.com/admin/pages/edit/5
并将其作为get变量附加。
所以在上面的例子中,我传递了一个请求的四个参数:
admin - request path / used to signify a login check must be done before passing
them on to their request
pages - This would be the class or object
edit - This would be the method called from the class / object
5 - This would be the actual row of the record in the database being edited
所以我开发了一个看起来像这样的路由器类:
class router {
public $url;
public $protectedPaths = array('admin','users','client');
public function __construct() {
$this -> url = explode('/', $_GET['url']);
if($this -> url[0] == '') {
$this -> loadDefaultView();
} else {
// Check to ensure that the path is not protected
if(in_array($this -> url[0], $this -> protectedPaths)) {
// check to ensure user is logged in
if($_COOKIE['isLogged']) {
// This means that there is no action or model needed just a returned view
if($this -> url[2] == '') {
$this -> loadViewWithoutAction();
} else {
// we check to ensure there is a controller
if(file_exists(baseControllers .'controller.'. $this -> url[1] .'.php')) {
// require that controller and instantiate it
require baseControllers .'controller.'. $this -> url[1] .'.php';
$obj = new $this -> url[1];
// check to see if method exists
if(method_exists($obj, $this -> url[2])) {
if($_POST) {
$data = $_POST;
} else {
$data = array($this -> url[3]);
}
// run method if necessary
$data = call_user_func_array(array($obj, $this -> url[2]), $data);
$this -> loadAdminView( $data );
} else {
$this -> loadErrorView();
}
} else {
$this -> loadErrorView();
}
}
} else {
header("Location: /auth/form");
}
} else {
// we check to ensure there is a controller
if(file_exists(baseControllers .'controller.'. $this -> url[0] .'.php')) {
// require that controller and instantiate it
require baseControllers .'controller.'. $this -> url[0] .'.php';
$obj = new $this -> url[0];
// check to see if method exists
if(method_exists($obj, $this -> url[1])) {
// run method if necessary
$data = call_user_func_array(array($obj, $this -> url[1]), array($this -> url[2]));
$this -> loadPublicView( $data );
} else {
$this -> loadErrorView();
}
} else {
$this -> loadErrorView();
}
}
}
}
所以我会使用一些不同的if else语句,也许还有一个开关来区分不同的请求等。最后我的问题是,自动加载一个类并运行一个方法是不好的做法。从我在框架中看到的,这都是手动的,我不是专家,所以我假设可能有这个原因。此外,我发现在网络上的OOP中自动化PHP请求几乎没有任何意义。
我真的想自动化这个,但同时我也不想引起安全问题。哦,对于用户输入的任何表格或个人信息,一切都将是邮寄或ajax,以防止黑客入侵网址。
提前感谢您的任何建议或解答!