我不认为有更好的方法,但这并不意味着没有。
阅读您的问题后的几点说明:
1) 对所有标题信息使用包装文件。所以,在页面的开头,输入:
require_once('package.php'); // that's what I call mine
然后在包装中,我有:
require_once('session.start.php');
require_once('db.con.php');
这样,您的所有页面都在访问相同的内容。如果您需要更改它,它会容易得多。
require_once、include_once、include和require之间存在速度差异。我不知道它有多重要。框架在制作页面时包含大约 60 多个文件,所以我一直认为它还不错。
会话信息存储在您服务器上的一个文件夹中。 PHP 将其默认为 /tmp(您应该将其更改为私人文件夹/不可通过网络访问)。
确保您正在验证发送到 AJAX 的所有信息。请记住,它就像它自己的网页一样,因此任何权限或数据库敏感信息都应受到同样的保护。
“我想我可以使用一个名为 page 的 ajax 并只包含一堆 case 语句,但仍然想知道是否有更好的方法。”
控制器模式非常适合这种类型的事情。在一个文件中包含一堆 case 语句对您的维护来说很困难。当您切换到只有 1 或 2 个函数的文件时,您的生活会变得简单得多。
根据您的项目规模,您可能希望实施一个框架。查看 MVC 框架。如果我不实现框架,我仍然会实现控制器模式。
我从我的博客中删除了这个。我现在用的根本不是这个样子,而是从这里开始的:
在表示层中,我正在确定要实现哪些元素。对于我想要实现的每个元素,我都会启动控制器,如下所示:
$controller = new Controller();
$context = $controller->getContext();
$context->addParam('action', 'login');
$template->setContent( $controller->process() );
我正在使用来自 Matt Zandstra 的 PHP Objects, Patterns, and Practice 3rd Ed 的控制器,并进行了我自己的修改。
会发生什么:
- 我的表示层获得了一个新的控制器对象。
- Controller 对象的构造函数自动创建一个新的 CommandContext 对象。
- CommandContext自动将请求变量作为参数加载,所以我什至不需要担心表单数据,直到我到达逻辑层并需要验证和处理它。
- 在表示层中,我加载了任何其他上下文参数(或我想要传递给控制器的信息),包括最重要的,我希望控制器执行的操作。
- 为了传递信息,我调用$controller->process()。在逻辑层,我可以使用默认的“执行”或执行不同的命令。因此,在表示层中,我将操作设置为“登录”,这会强制登录命令和登录视图页面打开,并且该命令默认为执行,但它可以是任何东西。
- 当我调用进程时,它会触发 CommandFactry。 CommandFactory 将首先启动一个新的 Template 子对象,例如侧栏 div 框或主体上下文。它使用我可以传递给控制器的可选标志来做出此决定。
- 然后,CommandFactory 将打开命令文件并将模板和上下文作为对象传递给逻辑层。
abstract class Command {
}
class CommandContext {
private $params = array();
private $error = "";
function __construct(){
$this->params = $_REQUEST;
}
function addParam( $key, $val ){
$this->params[$key] = $val;
}
function get( $key ){
return $this->params[$key];
}
function issetCheck( $key ){
if( ! empty( $this->params[$key] ) ){
return true;
}
return false;
}
function setError( $error ){
$this->error = $error;
}
function getError(){
return $this->error;
}
}
class CommandNotFoundException extends Exception { }
class CommandFactory {
private static $dir = 'include/classes/command/';
static function getCommand( $action = 'Default', $flag = 0 ){
switch( $flag ){
case 1:
$template = new TemplateQuickViewOnly();
break;
case 2:
$template = new TemplateQuickViewToggle();
break;
default:
$template = new TemplateMainBodyOnly();
break;
}
if( preg_match ( '/\W/', $action ) ){
throw new Exception("illegal characters in action");
}
$class = UCFirst(strtolower($action))."Command";
$file = ROOT_PATH."".self::$dir."{$class}.php";
if( ! file_exists( $file ) ){
throw new CommandNotFoundException( "could not find '$file'" );
}
include_once( $file );
if( ! class_exists($class) ){
throw new CommandNotFoundException( "no '$class' class located" );
}
$cmd = new $class( $template );
return array( $cmd, $template );
}
}
class Controller {
private $context;
function __construct(){
$this->context = new CommandContext();
}
function getContext(){
return $this->context;
}
function process( $method = 'execute', $flag = 0 ){
list( $cmd, $template ) = CommandFactory::getCommand( $this->context->get('action'), $flag );
if( ! $cmd->$method( $this->context ) ){
// handle failure
// $template->setMessage( UCFirst($this->context->get('action')).' failed to execute.');
return $template->getMessage();
}else{
// success dispatch view
return $template->getMessage();
}
}
}
逻辑层位于固定目录中。对象的一个实例已经被控制器层实例化,这意味着构造函数已经被触发。此外,控制器层已经调用了方法“execute”(默认)或其他方法,例如“getLoginForm”。另外,请注意,当 Controller 调用方法“execute”时,它也会将 CommandContext 传递给该方法,因此我们可以使用一些东西。
class LoginCommand extends Command {
public function __construct( ){ }
function execute ( CommandContext $context ){
if( $context->get('login_user_name') == 'demo' ){
$this->view->setMessage('Success is true!');
return true;
}
return false;
}
function getLoginForm( CommandContext $context ){
$this->view->setMessage('Second sucess is even more true!');
return true;
}
}