ECO是PHP 5.5+的PHP框架
安裝示例:
# wget shayanderson.com/eco.zip
# unzip ./eco.zip
可以在app/com/route.php中設置基本路由,例如:
// set routes
return [
// class method
' / ' => ' IndexController->home ' ,
// namespace
' login ' => ' AccountUserController->login ' ,
// callable
' logout ' => function () { /* do something */ },
// more routes
];請求/將在File app/mod/IndexController.php中的類IndexController中調用方法home 。
請求/login將在File app/mod/Account/UserController.php中的類AccountUserController中調用方法login 。
特定的HTTP方法可用於路由:
eco:: route ( ' GET@api/user/:id ' , function ( $ id ) { /* GET method */ });
eco:: route ( ' DELETE@api/user/:id ' , function ( $ id ) { /* DELETE method */ });
eco:: route ( ' POST@api/user/:id? ' , function ( $ id = null ) { /* POST method */ });
// or handle multiple HTTP methods
eco:: route ( ' POST|PUT@api/user/:id? ' , function ( $ id = null ) { /* POST or PUT method */ });支持這些HTTP方法:
DELETE,GET,HEAD,PATCH,POST,PUT
路由回調可以是可呼叫的名稱或可呼叫(第一個數組元素必須是控制器/操作):
function auth () { /* do something */ }
eco:: route ( ' account/secure ' ,
[ ' AccountController->secure ' , ' auth ' , function () { /* invoked */ }]);警告:路由回調不適用於動態路線設置
動態路由設置器可用於具有許多路由(懶負載路由)的較大應用程序,例如:
eco:: route ( ' account* ' , ' AccountController::getRoutes() ' );現在,以/account開頭的所有請求將使用該方法加載路線:
class AccountController
{
public static function getRoutes ()
{
return [
' account/login ' => ' AccountController->login ' ,
// do not have to use class name if in same class:
' account/logout ' => ' logout '
];
}
}動態路由設置(懶負載路由)也可以直接設置為數組,例如:
eco:: route ( ' account* ' , [
' AccountController ' , // the class name must be first (index:0)
' account/login ' => ' login ' ,
' account/logout ' => ' logout '
]);CLI路由很容易使用,例如:
eco: route ( ' bin/test/:id ' , function ( $ id ) {
echo ' CLI test, id: ' . $ id ;
});現在可以發出CLI命令:
$ php index.php bin/test/5
Bin test: 5
上述路線也將在Web瀏覽器中工作。為了創建僅CLI路由(將在Web瀏覽器中不起作用)為路由的開頭添加
$:例如:
// this route will only work as CLI command
// the request '/bin/test/5' in a Web browser would result in a 404 error
eco: route ( ' $bin/test/:id ' , function ( $ id ) {
echo ' CLI test, id: ' . $ id ;
});命名路由參數:
eco:: route ( ' user/:id ' , ' UserController->view ' ); :id值將傳遞給可呼叫或類方法:
class UserController
{
public function view ( $ id )
{
// do something
}
}可選參數可以使用:
eco:: route ( ' page/:id/:title? ' , function ( $ id , $ title = null ) {});通配符參數示例:
eco:: route ( ' product/:info+ ' , function ( $ info ) {
// if request is '/product/1/abc/x'
// $info is an array: ['1', 'abc', 'x']
});路由參數回調可以以幾種不同的方式使用:
eco:: route ( ' page/:title:strtoupper ' , ' PageController->view ' );或在數組中設置參數回調(第一個數組元素必須是類方法 /可呼叫):
eco:: route ( ' page/:title ' , [ ' PageController->view ' , [ ' title ' => ' strtoupper ' ]]);另外,可以設置全局路由參數回調,例如:
// globally set route param callback
// now every ':id' param will use this callback
// global param callbacks are overwritten by local ones
eco:: param ( ' id ' , function ( $ id ) { return strtoupper ( $ id ); });
// or use multiple param names:
eco:: param ([ ' x ' , ' y ' , ' z ' ], function ( $ val ) { });路由回調也可以與路由參數回調一起使用:
eco:: route ( ' page/:title ' , [
' PageController->view ' ,
' route_callback ' ,
function () { /* route callback */ },
// route param callback
[ ' title ' => ' strtoupper ' ]
]);可以使用路由參數正則表達式,如果匹配失敗,則不會調用路由:
eco:: route ( ' user/:id@[0-9]+ ' , ...);使用不同類型的參數和/或組合回調和正則表達式時,語法順序很重要,這是正確的語法:
// when using callback with regular expression
// the callback must come first:
eco:: route ( ' user/:id:strtoupper@[0-9]+ ' , ...);
// use optional param and callback:
eco:: route ( ' user/:id/:name?:strtoupper ' , ...);
// use optional param and regular expression:
eco:: route ( ' user/:id/:name?@[a-z]+ ' , ...);
// use optional param, callback and regular expression:
eco:: route ( ' user/:id/:name?:strtoupper@[a-z]+ ' , ...);
// use wildcard param and callback:
eco:: route ( ' user/:params+:strtoupper ' , ...);
// use wildcard param and regular expression:
eco:: route ( ' user/:params+@[a-z]+ ' , ...);
// use wildcard param, callback and regular expression:
eco:: route ( ' user/:params+:strtoupper@[a-z]+ ' , ...);視圖對象可以在路由操作中使用:
class PageController
{
public function get ( $ id )
{
$ page = PageModel:: get ( $ id );
// set view param
eco:: view ()-> set ( ' title ' , $ page -> title );
// or use view() helper function
// view()->set('title', $page->title);
// or like this:
view ()-> author = $ page -> author ;
// load template file 'page/view'
// and can also set view param 'content'
view ( ' page/view ' , [
' content ' => $ page -> content
]);
}
}路由參數也可以使用
Router類方法訪問。
app/tpl/page/view.tpl文件示例:
<?php include PATH_TEMPLATE_GLOBAL . ' header.tpl ' ; ?>
<h1> <?= $ title ?> </h1>
<p>Author: <?= $ author ?> </p>
<?= $ content ?>
<?php include PATH_TEMPLATE_GLOBAL . ' footer.tpl ' ; ?> 記錄消息示例:
eco:: log ()-> error ( ' Bad error / fatal ' );
eco:: log ()-> warning ( ' Warning conditions ' );
eco:: log ()-> notice ( ' Notice message ' );
eco:: log ()-> debug ( ' File has loaded ' );記錄消息時可以使用類別:
// category 'payment'
eco:: log ()-> error ( ' Failed to access payment gateway ' , ' payment ' );還可以添加調試信息:
eco:: log ()-> warning ( ' Request failed ' , /* category optional */ null ,
[ ' request ' => ' x ' , ' client ' => ' y ' ]);獲取整個日誌:
$ log = eco:: log ()-> get ();設置自定義日誌處理程序示例:
eco:: log ()-> setHandler ( function ( $ message , $ level , $ category , $ info ){
// if sending message to database
// do no send if database connection error, example:
// if(substr($message, 0, 17) == 'SQLSTATE[HY000] [')
// {
// return false;
// }
// handle custom logging
return true ; // handled
});如果自定義日誌處理程序無法返回
true則內部記錄器將按照正常繼續繼續
觸發錯誤示例:
eco:: error ( ' Error message ' );
// or use helper function
error ( ' Something bad ' );
// custom error code
error ( ' Page not found ' , 404 );
// add log category
error ( ' Failed to load user ' , null , ' account ' );
// by default the HTTP error response code is sent
// to not send HTTP code use:
error ( ' Page not found ' , 404 , ' category ' false ); eco::error()使用:
$ error_message = eco:: errorGetLast ();
$ error_category = eco:: errorGetLastCategory ();在調用錯誤之前,可以使用404回調來處理404
鉤子可用於加載文件或在執行過程中使用回調,例如:
// called before routing starts
eco:: hook (eco:: HOOK_BEFORE , function () { /* do something */ });
// called before the route callable or class method is called
// include a file example:
eco:: hook (eco:: HOOK_MIDDLE , PATH_LIB . ' hook/middle.php ' );
// called after dispatching
eco:: hook (eco:: HOOK_AFTER , function () { /* do something */ });應用程序和ECO配置設置分別處理。
ECO配置設置文件位於app/com/conf/eco.conf.php上,並包含所有框架設置。所有有關ECO配置設置的文檔都可以在文件中找到。
應用程序配置設置可以通過ECO存儲,例如:
// load config file(s) using path
eco:: conf ( PATH_CONF . ' app.conf.php ' );
eco:: conf ( PATH_CONF . ' api.conf.php ' );
// or load config settings using array
eco:: conf ([ ' local ' => [ ' path ' => ' x ' , ' filename ' => ' y ' ]]);
// use config settings
$ app_username = eco:: conf ()-> app -> username ;
$ local_path = eco:: conf ()-> local -> path ;配置文件必須返回數組, app.conf.php示例:
return [
' app ' => [
' username ' => ' x ' ,
' password ' => ' y '
],
' core ' => [ /* more */ ]
];使用多個文件時,配置設置不能被覆蓋,因此即使在不同的文件中,主數組鍵也必須是唯一的。切勿使用ECO用於內部框架設置的主數組密鑰
_eco。
配置設置也可以與ECO分開使用,例如:
// do not store internally
$ conf = eco:: conf ( PATH_CONF . ' app.conf.php ' , false );
// use config settings
$ app_username = $ conf -> app -> username ;ECO提供以下方法:
eco::autoload($paths) - 類自動加載器eco::breadcrumb() - 訪問麵包屑類( breadcrumb()可用的輔助功能)eco::clear($key) - 清除全局變量eco::conf($file_path, $store) - 註冊 /加載應用程序配置設置文件( conf()助手功能可用)eco::db($connection_id) - 訪問數據庫類( db()輔助功能可用)eco::error($message, $code, $category, $http_response_code) - 觸發錯誤( error error()助手功能可用)eco::filter() - 訪問數據過濾器類( filter()輔助功能可用)eco::flash() - 訪問會話flash類( flash()輔助功能可用)eco::format() - 訪問數據格式類( format()輔助功能可用)eco::get($key) - 獲取全局變量eco::has($key) - 檢查是否存在全局變量eco::hook($name, $callback) - 添加鉤子eco::log() - 訪問日誌類( logger()可用輔助功能)eco::model() - 模型類加載程序( model()輔助功能可用)eco::param($id, $callback) - 地圖路由參數回調( param()可用輔助功能)eco::request() - 訪問請求類( request()可用輔助功能)eco::response() - 訪問響應類( response()輔助功能可用)eco::route($route, $action) - 地圖路線eco::router() - 訪問核心路由器類eco::run() - 運行應用程序eco::service() - 服務類Loader( service()可用輔助功能)eco::session() - 訪問會話類( session()助手功能可用)eco::set($key, $value) - 設置全局變量eco::stop() - 優雅地停止應用程序( stop()輔助功能)eco::validate() - 訪問數據驗證類( validate()輔助功能可用)eco::view($template, $view_params) - 加載視圖模板文件和訪問視圖類( view()輔助功能可用) 核心類可用於簡化常見的應用程序任務:
EcoCache服務器端緩存核心類EcoDatabase - 數據庫核心類EcoForm -html表格核心類EcoHttp -HTTP請求核心類EcoModel - 數據庫模型核心類EcoTable -HTML表核心類這些輔助課程可用:
EcoBenchmark基準輔助器EcoSystemBreadcrumb與eco::breadcrumb()或breadcrumb()輔助功能訪問EcoSystemFilter eco::filter()或filter()輔助功能訪問EcoSystemSessionFlash使用eco::flash()或flash()輔助功能訪問EcoSystemFormat - 使用eco::format()或format()輔助功能訪問EcoSystemRequest eco::request()或request()輔助功能訪問EcoSystemRegistryService使用eco::service()或service()助手函數訪問EcoSystemSession通過eco::session()或session()助手函數訪問EcoSocketClient和EcoSocketServer創建套接字幫助者EcoSystemValidate -acccess with eco::validate()或validate()輔助功能助手功能可用於快速訪問Eco Core方法或其他有用功能。在此處找到助手功能文檔。
可以通過擴展核心EcoSystem類來擴展ECO。最初,這是app/com/bootstrap.php文件中的設置:
// set Eco access class
class eco extends Eco System {}擴展類可用於添加方法或覆蓋EcoSystem超固化方法。
^回到頂部