例如,一个简单的MVC类型的系统:
使用.htaccess/nginx.conf将/api/class/method重写为PHP变量,然后执行以下操作:
这是一种极不好的做法吗?如果这是不好的做法,有人能够解释一下为什么吗?如果是的话,那么有没有更好的方法来实现我的目标?
编辑:本质上,我使用可变变量是为了扩展核心系统变得更加简单 - 例如 - 添加一个新控制器非常容易。我完全理解允许任何函数或类被实例化存在的安全风险,因此应该加入某种过滤器。
“some_filter_here”可以是允许的控制器列表 - 白名单,正如一些人在这里提到的那样。
使用.htaccess/nginx.conf将/api/class/method重写为PHP变量,然后执行以下操作:
<?php
// Set up class + method variables
$className = some_class_filter($_GET['class']);
$method = some_method_filter($_GET['method']);
// Check if class exists and execute
if(file_exists(BASE . "/controllers/" . $className . ".class.php")) {
require BASE . "/controllers/" . $className . ".class.php";
$$className = new $className();
// Execute the method
$$className->$method();
} else {
// Spit out some error based on the problem
}
?>
这是一种极不好的做法吗?如果这是不好的做法,有人能够解释一下为什么吗?如果是的话,那么有没有更好的方法来实现我的目标?
编辑:本质上,我使用可变变量是为了扩展核心系统变得更加简单 - 例如 - 添加一个新控制器非常容易。我完全理解允许任何函数或类被实例化存在的安全风险,因此应该加入某种过滤器。
“some_filter_here”可以是允许的控制器列表 - 白名单,正如一些人在这里提到的那样。
$$className = new $className();
。 - Lightness Races in Orbit