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"
}