2014-10-10 73 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); 
}