2014-10-10 75 views
0

我有一个在MVC模式和smarty模板引擎的应用程序。 和我没有任何问题与Apache和重写引擎。 我试图通过这个来处理我的代码的URL和加载内容: 我拆分请求$req阵列:处理虚拟url中的内容的最佳方式 - php

... load all libs and function required . 

$smarty->display('header.tpl'); 

// main switch case 

switch ($req[0]) { 
    case 'index.php': 
     require './inc/index.php'; 
     $smarty->display('index.tpl'); 
     break; 
    case 'list': 
     $smarty->display('list.tpl'); 
     break; 
    case 'topic': 
     $smarty->display('single.tpl'); 
     break; 
    case 'login': 
     require_once './inc/login.php'; 
     $smarty->display('login.tpl'); 
     break; 

    case 'msg': 
     $smarty->assign('msg', $req[1]); 
     $smarty->display('message.tpl'); 
     break; 

    default: 
     $smarty->display('error.tpl'); 
     break; 
} 

$smarty->display('footer.tpl'); 

现在,我只是觉得我的代码没有优化和标新立异,太慢, 如何优化此代码(总是$ req [0]不是ASCII)?

+0

嗯,你的做法是好的,只是没有这样做正是这样,因为你发现了,在开关情况可能会很长。 如何添加处理路由URL到内容的抽象层?在最低限度,你可以有一个req [0]到tpls的哈希表(assoc array)。但从我的经验来看,smarty本来就比普通的php慢。你确定缓慢是由开关盒引起的吗? – Patrick 2014-10-10 18:58:53

+0

我不确定,但想一想。 – 2014-10-10 20:08:34

回答

1

最好的和快速的方法是通过REQ包括[0]您需要通过文件的一些模式:

$fname = './inc/' . $req[0] . '.php'; 
if (file_exists($fname)) { 
include($fname); 
}