FlEAPHP + Smarty 進行php開發

1、首先下載FLEAPHP和Smarty的源碼,解壓即是安裝。

 

2、建立開發根目錄:jccode

 

其結構如下:

 

docs/  flea/  logs/  smarty/

 

其中docs是web的根目錄。

 

 

flea下是與flea相關的代碼,其中flea/lib就是flea的安裝目錄。而flea/app下包含了我們的網站邏輯:controller,以及配置(config)、函數及類(helper)、數據庫相關文件(table)。

 

[flykobe@localhost jccode]$ ls flea/
app  lib
[flykobe@localhost jccode]$ ls flea/app/
config  Controller  helper  table

[flykobe@localhost jccode]$ ls flea/lib/
3rd  _Cache  FLEA  FLEA.php  LICENSE.txt  RELEASE-PACK.TXT

 

爲了把flea和smarty結合起來,關鍵是flea/app/config/config.php文件:

 

<?php
return array(
    'defaultController' => 'Default',

    'defaultLanguage' => 'utf-8',
    'urlMode' => 'URL_PATHINFO',
    'autoLoad' => array(), // 自動載入的文件
    'dbDSN' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'login'     => 'root',
        'password'  => '123456',
        'database'  => 'GUESTBOOK',

        'charset'   => 'utf-8',
    ),
    'logFileDir' => "../logs",
    'displayErrors' => true,
    'friendlyErrorsMessage' => true,
    'view' => 'FLEA_View_Smarty',
    'viewConfig' => array(
        'smartyDir'         => "/usr/lib/php/smarty/",
        'template_dir'      => '/var/www/html/jccode/smarty/templates/',
        'compile_dir'       => '/var/www/html/jccode/smarty/templates_c/',
        'left_delimiter'    => '{{',
        'right_delimiter'   => '}}',
    ),
);

 

其中的view和viewConfig是與smarty相關的部分。

 

爲了能夠在生成smarty對象的時候加載這些配置,需要讓controller繼承FLEA_Controller_Action:

 

class Controller_Default extends FLEA_Controller_Action
{
    var $smarty;        
    function __construct(){  
        $this->smarty =& $this->_getView();
        parent::__construct('Controller_Default');
    }
   
    function actionIndex() {
        $this->smarty->assign('name', 'Ned');
        $this->smarty->display('index.tpl');
    }                   
}

 

同時,flea中有一個bug,需要在FLEA/View/Smarty.php的構造函數中,修改如下:

 

   function FLEA_View_Smarty() {
        // cy:  change smarty to construct
        // parent::Smarty();    
        parent::__construct();

        。。。。

    }

 

這樣,jccode/docs/index.php的內容如下即可:

 

<?php
require("../flea/lib/FLEA.php");

define("APP_DIR", "../flea/app/");
FLEA::import(APP_DIR); 
 
FLEA::loadAppInf(APP_DIR . "config/config.php");
 
FLEA::runMVC();     

 

另外,附上index.tpl的內容:

 

<html>
<head>
<title>Smarty</title>
</head>
<body>
Hello, {{$name}}!      
</body>                
</html>

 

 

另外,不知道是不是因爲我下載過程中的編碼問題,下面打印出的日期總是亂碼:

<td align="right"><?php echo $_smarty_tpl->smarty->plugin_handler->executeModifier('date_format',array($_smarty_tpl->get
Variable('entry')->value['EntryDate'],"%e %b, %Y %H:%M:%S"),true);?>

 

查看smarty源碼: plugins/modifier.date_format.php的smarty_modifier_date_format函數,最後是使用了strftime來格式化時間的,所以在我的index.php中添加了以下環境變量的設置就好了:

setlocale(LC_TIME, 'zh_CN.UTF8');

 

附上FLEAPHP的global變量,這樣如果需要修改其中的某些值的話,直接在config文件中賦值就可以了:

array(6) {
["APP_INF"]=>
array(74) {
["defaultTimezone"]=>
NULL
["MVCPackageFilename"]=>
string(56) "/var/www/html/jccode/flea/lib/FLEA/Controller/Action.php"
["controllerAccessor"]=>
string(10) "controller"
["defaultController"]=>
string(7) "Default"
["actionAccessor"]=>
string(6) "action"
["defaultAction"]=>
string(5) "index"
["urlMode"]=>
string(12) "URL_PATHINFO"
["urlBootstrap"]=>
string(9) "index.php"
["urlAlwaysUseBootstrap"]=>
bool(true)
["urlAlwaysUseAccessor"]=>
bool(true)
["urlParameterPairStyle"]=>
string(1) "/"
["urlLowerChar"]=>
bool(false)
["urlCallback"]=>
NULL
["controllerClassPrefix"]=>
string(11) "Controller_"
["actionMethodPrefix"]=>
string(6) "action"
["actionMethodSuffix"]=>
string(0) ""
["dispatcher"]=>
string(22) "FLEA_Dispatcher_Simple"
["dispatcherFailedCallback"]=>
NULL
["internalCacheDir"]=>
NULL
["autoLoad"]=>
array(0) {
}
["sessionProvider"]=>
NULL
["autoSessionStart"]=>
bool(true)
["requestFilters"]=>
array(0) {
}
["dbDSN"]=>
array(6) {
["driver"]=>
string(5) "mysql"
["host"]=>
string(9) "localhost"
["login"]=>
string(4) "root"
["password"]=>
string(6) "123456"
["database"]=>
string(9) "GUESTBOOK"
["charset"]=>
string(5) "utf-8"
}
["dbTDGAutoInit"]=>
bool(true)
["dbTablePrefix"]=>
string(0) ""
["dbMetaLifetime"]=>
int(10)
["dbMetaCached"]=>
bool(true)
["view"]=>
string(16) "FLEA_View_Smarty"
["viewConfig"]=>
array(5) {
["smartyDir"]=>
string(20) "/usr/lib/php/smarty/"
["template_dir"]=>
string(38) "/var/www/html/jccode/smarty/templates/"
["compile_dir"]=>
string(40) "/var/www/html/jccode/smarty/templates_c/"
["left_delimiter"]=>
string(2) "{{"
["right_delimiter"]=>
string(2) "}}"
}
["ajaxClassName"]=>
string(9) "FLEA_Ajax"
["webControlsClassName"]=>
string(16) "FLEA_WebControls"
["webControlsExtendsDir"]=>
NULL
["responseCharset"]=>
string(5) "utf-8"
["databaseCharset"]=>
string(4) "utf8"
["autoResponseHeader"]=>
bool(true)
["multiLanguageSupport"]=>
bool(false)
["languageSupportProvider"]=>
string(13) "FLEA_Language"
["languageFilesDir"]=>
NULL
["defaultLanguage"]=>
string(5) "utf-8"
["autoLoadLanguage"]=>
NULL
["dispatcherAuthProvider"]=>
string(9) "FLEA_Rbac"
["defaultControllerACTFile"]=>
string(0) ""
["autoQueryDefaultACTFile"]=>
bool(false)
["controllerACTLoadWarning"]=>
bool(true)
["defaultControllerACT"]=>
NULL
["globalACT"]=>
NULL
["dispatcherAuthFailedCallback"]=>
NULL
["RBACSessionKey"]=>
string(13) "RBAC_USERDATA"
["logEnabled"]=>
bool(true)
["logProvider"]=>
string(8) "FLEA_Log"
["logFileDir"]=>
string(7) "../logs"
["logFilename"]=>
string(10) "access.log"
["logFileMaxSize"]=>
int(4096)
["logErrorLevel"]=>
string(45) "notice, debug, warning, error, exception, log"
["exceptionHandler"]=>
string(24) "__FLEA_EXCEPTION_HANDLER"
["displayErrors"]=>
bool(true)
["friendlyErrorsMessage"]=>
bool(true)
["displaySource"]=>
bool(true)
["helper.verifier"]=>
string(20) "FLEA_Helper_Verifier"
["helper.encryption"]=>
string(22) "FLEA_Helper_Encryption"
["helper.array"]=>
string(17) "FLEA_Helper_Array"
["helper.file"]=>
string(22) "FLEA_Helper_FileSystem"
["helper.image"]=>
string(17) "FLEA_Helper_Image"
["helper.pager"]=>
string(17) "FLEA_Helper_Pager"
["helper.uploader"]=>
string(24) "FLEA_Helper_FileUploader"
["helper.yaml"]=>
string(16) "FLEA_Helper_Yaml"
["helper.html"]=>
string(16) "FLEA_Helper_Html"
["sessionDbDSN"]=>
string(5) "dbDSN"
["sessionDbTableName"]=>
string(8) "sessions"
["sessionDbFieldId"]=>
string(7) "sess_id"
["sessionDbFieldData"]=>
string(9) "sess_data"
["sessionDbFieldActivity"]=>
string(8) "activity"
["sessionDbLifeTime"]=>
int(1440)
}
["OBJECTS"]=>
array(0) {
}
["DBO"]=>
array(0) {
}
["CLASS_PATH"]=>
array(2) {
[0]=>
string(29) "/var/www/html/jccode/flea/lib"
[1]=>
string(29) "/var/www/html/jccode/flea/app"
}
["FLEA_EXCEPTION_STACK"]=>
array(0) {
}
["FLEA_EXCEPTION_HANDLER"]=>
string(24) "__FLEA_EXCEPTION_HANDLER"
}
發佈了179 篇原創文章 · 獲贊 2 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章