ECO เป็นเฟรมเวิร์ก PHP สำหรับ PHP 5.5+
ตัวอย่างติดตั้ง:
# 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
]; คำขอ / จะเรียกวิธีการ home ในคลาส IndexController ใน app/mod/IndexController.php
คำขอ /login จะเรียกวิธี login ในคลาส AccountUserController ใน app/mod/Account/UserController.php
วิธี 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
เส้นทางข้างต้นจะทำงานในเว็บเบราว์เซอร์ ในการสร้างเส้นทาง CLI เท่านั้น (จะไม่ทำงานในเว็บเบราว์เซอร์) เพิ่ม
$ให้กับจุดเริ่มต้นของเส้นทางตัวอย่างเช่น:
// 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 ' );หรือตั้งค่าการโทรกลับพารามิเตอร์ในอาร์เรย์ (องค์ประกอบอาร์เรย์แรกจะต้องเป็นวิธีคลาส / callable):
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
});หาก callable handler บันทึกที่กำหนดเองไม่ส่งคืน
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) - คลาส autoloader คลาสeco::breadcrumb() - เข้าถึง Breadcrumb Class ( breadcrumb() ฟังก์ชั่น Helper ที่มีอยู่)eco::clear($key) - ล้างตัวแปรทั่วโลกeco::conf($file_path, $store) - ลงทะเบียน / โหลดแอปพลิเคชันการกำหนดค่าการกำหนดค่าไฟล์ ( conf() ฟังก์ชั่นผู้ช่วย)eco::db($connection_id) - การเข้าถึงคลาสฐานข้อมูลคลาส ( db() ฟังก์ชั่นตัวช่วยที่มีอยู่)eco::error($message, $code, $category, $http_response_code) - ทริกเกอร์ข้อผิดพลาด ( error() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::filter() - การเข้าถึงข้อมูลตัวกรองข้อมูล ( filter() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::flash() - การเข้าถึงเซสชันคลาสแฟลช ( flash() ฟังก์ชั่นตัวช่วย) พร้อมใช้งาน)eco::format() - การเข้าถึงคลาสข้อมูลรูปแบบ ( format() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::get($key) - รับตัวแปรทั่วโลกeco::has($key) - ตรวจสอบว่ามีตัวแปรทั่วโลกอยู่หรือไม่eco::hook($name, $callback) - เพิ่ม hookeco::log() - คลาสบันทึกการเข้าถึง ( logger() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::model() - โมเดลคลาสโหลดเดอร์ ( model() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::param($id, $callback) - แผนที่เส้นทางการเรียกพารามิเตอร์การโทรกลับ ( param() ฟังก์ชั่นผู้ช่วย)eco::request() - คลาสคำขอเข้าถึง ( request() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::response() - Access Response Class ( response() ฟังก์ชั่น Helper มีให้)eco::route($route, $action) - เส้นทางแผนที่eco::router() - Access Core Router Classeco::run() - เรียกใช้แอปพลิเคชันeco::service() - Service Class Loader ( service() ฟังก์ชั่น Helper มีให้)eco::session() - การเข้าถึงคลาสเซสชัน ( session() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::set($key, $value) - ตั้งค่าตัวแปรส่วนกลางeco::stop() - หยุดแอปพลิเคชัน ( stop() ฟังก์ชั่นผู้ช่วยอย่างสง่างาม)eco::validate() - การเข้าถึงข้อมูลการตรวจสอบความถูกต้องของคลาส ( validate() ฟังก์ชั่นผู้ช่วยที่มีอยู่)eco::view($template, $view_params) - โหลดไฟล์ดูเทมเพลตและการเข้าถึงคลาสดู ( view() ฟังก์ชั่นตัวช่วย) พร้อมใช้งาน) คลาสหลักสามารถใช้เพื่อทำให้งานแอปพลิเคชันทั่วไปง่ายขึ้น:
EcoCache - คลาสแคชแคชด้านเซิร์ฟเวอร์EcoDatabase - คลาส Core DatabaseEcoForm - HTML Form Core ClassEcoHttp - HTTP Request Core ClassEcoModel - คลาส CORE MODEL DATABASEEcoTable - HTML Table Core Class คลาสผู้ช่วยเหล่านี้มีอยู่:
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() Helper FunctionEcoSocketClient และ EcoSocketServer - การสร้างซ็อกเก็ตผู้ช่วยEcoSystemValidate - acccess ด้วย eco::validate() หรือ validate() ฟังก์ชั่นผู้ช่วย ฟังก์ชั่นผู้ช่วยสามารถใช้ในการเข้าถึงวิธีการ Eco Core หรือฟังก์ชั่นที่เป็นประโยชน์อื่น ๆ ได้อย่างรวดเร็ว ค้นหาเอกสารฟังก์ชั่นผู้ช่วยที่นี่
Eco สามารถขยายได้โดยการขยายคลาส Core EcoSystem ตอนแรกนี่คือการตั้งค่าใน app/com/bootstrap.php ไฟล์:
// set Eco access class
class eco extends Eco System {} คลาสขยายสามารถใช้เพื่อเพิ่มวิธีการหรือแทนที่ EcoSystem วิธีการที่ใช้งานได้
^ กลับไปด้านบน