ECO adalah kerangka PHP untuk PHP 5.5+
Instal Contoh:
# wget shayanderson.com/eco.zip
# unzip ./eco.zip
Rute dasar dapat diatur di app/com/route.php , misalnya:
// set routes
return [
// class method
' / ' => ' IndexController->home ' ,
// namespace
' login ' => ' AccountUserController->login ' ,
// callable
' logout ' => function () { /* do something */ },
// more routes
]; Permintaan / akan memanggil home metode di kelas IndexController di File app/mod/IndexController.php .
Permintaan /login akan memanggil metode login di Class AccountUserController di File app/mod/Account/UserController.php .
Metode HTTP spesifik dapat digunakan untuk rute:
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 */ });Metode HTTP ini didukung:
DELETE,GET,HEAD,PATCH,POST,PUT
Rute Callbacks dapat berupa nama yang dapat dipanggil atau dapat dipanggil (elemen array pertama harus controller/action):
function auth () { /* do something */ }
eco:: route ( ' account/secure ' ,
[ ' AccountController->secure ' , ' auth ' , function () { /* invoked */ }]);Peringatan: Rute Callbacks tidak bekerja dengan pengaturan rute yang dinamis
Setter rute dinamis dapat digunakan untuk aplikasi yang lebih besar yang memiliki banyak rute (rute beban malas), misalnya:
eco:: route ( ' account* ' , ' AccountController::getRoutes() ' ); Sekarang semua permintaan yang dimulai dengan /account akan memuat rute menggunakan metode ini:
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 '
];
}
}Pengaturan rute dinamis (rute beban malas) juga dapat diatur secara langsung sebagai array, contoh:
eco:: route ( ' account* ' , [
' AccountController ' , // the class name must be first (index:0)
' account/login ' => ' login ' ,
' account/logout ' => ' logout '
]);Routing CLI mudah digunakan, misalnya:
eco: route ( ' bin/test/:id ' , function ( $ id ) {
echo ' CLI test, id: ' . $ id ;
});Sekarang perintah CLI dapat dikeluarkan:
$ php index.php bin/test/5
Bin test: 5
Rute di atas juga akan bekerja di browser web. Untuk membuat rute CLI Only (tidak akan berfungsi di browser web) Tambahkan
$ke awal rute, misalnya:
// 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 ;
});Parameter rute bernama:
eco:: route ( ' user/:id ' , ' UserController->view ' ); Nilai :id akan diteruskan ke metode yang dapat dipanggil atau kelas:
class UserController
{
public function view ( $ id )
{
// do something
}
}Parameter opsional dapat digunakan:
eco:: route ( ' page/:id/:title? ' , function ( $ id , $ title = null ) {});Contoh parameter wildcard:
eco:: route ( ' product/:info+ ' , function ( $ info ) {
// if request is '/product/1/abc/x'
// $info is an array: ['1', 'abc', 'x']
});Callback Parameter Rute dapat digunakan dalam beberapa cara berbeda:
eco:: route ( ' page/:title:strtoupper ' , ' PageController->view ' );Atau atur panggilan balik parameter di array (elemen array pertama harus metode kelas / dapat dipanggil):
eco:: route ( ' page/:title ' , [ ' PageController->view ' , [ ' title ' => ' strtoupper ' ]]);Juga, panggilan balik parameter rute global dapat diatur, misalnya:
// 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 ) { });Rute Callbacks juga dapat digunakan dengan panggilan balik parameter rute:
eco:: route ( ' page/:title ' , [
' PageController->view ' ,
' route_callback ' ,
function () { /* route callback */ },
// route param callback
[ ' title ' => ' strtoupper ' ]
]);Parameter Rute Ekspresi reguler dapat digunakan, jika pertandingan gagal, rute tidak akan dipanggil:
eco:: route ( ' user/:id@[0-9]+ ' , ...);Urutan sintaks penting saat menggunakan berbagai jenis parameter dan/atau menggabungkan panggilan balik dan ekspresi reguler, berikut adalah sintaks yang benar:
// 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]+ ' , ...);Objek tampilan dapat digunakan dalam tindakan rute:
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
]);
}
}Parameter rute juga dapat diakses menggunakan metode kelas
Router.
Contoh file 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 ' ; ?> Contoh Pesan Logging:
eco:: log ()-> error ( ' Bad error / fatal ' );
eco:: log ()-> warning ( ' Warning conditions ' );
eco:: log ()-> notice ( ' Notice message ' );
eco:: log ()-> debug ( ' File has loaded ' );Kategori dapat digunakan saat mencatat pesan:
// category 'payment'
eco:: log ()-> error ( ' Failed to access payment gateway ' , ' payment ' );Info debugging juga dapat ditambahkan:
eco:: log ()-> warning ( ' Request failed ' , /* category optional */ null ,
[ ' request ' => ' x ' , ' client ' => ' y ' ]);Dapatkan seluruh log:
$ log = eco:: log ()-> get ();Mengatur Contoh Penangan Log Kustom:
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
});Jika penangan log khusus yang dapat dipanggil tidak mengembalikan
trueloger internal akan berlanjut seperti biasa
Contoh kesalahan pemicu:
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 ); Pesan kesalahan terakhir (dan kategori kesalahan) yang dikirim ke fungsi eco::error() dapat diakses menggunakan:
$ error_message = eco:: errorGetLast ();
$ error_category = eco:: errorGetLastCategory ();Callback 404 dapat digunakan untuk menangani 404 sebelum kesalahan dipanggil
Hooks dapat digunakan untuk memuat file atau menggunakan panggilan balik selama eksekusi, contoh:
// 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 */ });Aplikasi dan pengaturan konfigurasi lingkungan ditangani secara terpisah.
File Pengaturan Konfigurasi ECO terletak di app/com/conf/eco.conf.php dan berisi semua pengaturan kerangka kerja. Semua dokumentasi untuk pengaturan konfigurasi lingkungan dapat ditemukan di file.
Pengaturan konfigurasi aplikasi dapat disimpan oleh ECO, Contoh:
// 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 ; File konfigurasi harus mengembalikan contoh array, app.conf.php :
return [
' app ' => [
' username ' => ' x ' ,
' password ' => ' y '
],
' core ' => [ /* more */ ]
];Pengaturan konfigurasi tidak dapat ditimpa saat menggunakan beberapa file, sehingga tombol array utama harus unik bahkan dalam file yang berbeda. Jangan pernah menggunakan kunci array utama
_ecoyang digunakan oleh ECO untuk pengaturan kerangka kerja internal.
Pengaturan konfigurasi juga dapat digunakan secara terpisah dari Eco, misalnya:
// do not store internally
$ conf = eco:: conf ( PATH_CONF . ' app.conf.php ' , false );
// use config settings
$ app_username = $ conf -> app -> username ;Eco menawarkan metode berikut:
eco::autoload($paths) - Kelas Autoloadereco::breadcrumb() - Akses Breadcrumb Class ( breadcrumb() Helper Function Tersedia)eco::clear($key) - CLEAR Variabel Globaleco::conf($file_path, $store) - Register / Load Application Configuration Settings File ( conf() Fungsi helper tersedia)eco::db($connection_id) - Kelas Access Database ( db() Fungsi Helper Tersedia)eco::error($message, $code, $category, $http_response_code) - Memicu Fungsi Kesalahan ( error() Helper tersedia)eco::filter() - Kelas Akses Data Filter ( filter() Fungsi Helper Tersedia)eco::flash() - Fungsi Kelas Flash Sesi Akses ( flash() Helper Function Tersedia)eco::format() - Kelas Format Data Akses ( format() Fungsi Pembantu Tersedia)eco::get($key) - Dapatkan variabel globaleco::has($key) - Periksa apakah ada variabel globaleco::hook($name, $callback) - Tambahkan kaiteco::log() - Access Log Class ( logger() Helper Function Tersedia)eco::model() - Model Class Loader ( model() Fungsi Helper Tersedia)eco::param($id, $callback) - Peta Parameter Rute Callback ( param() Fungsi Helper Tersedia)eco::request() - Kelas Permintaan Akses ( request() Fungsi Pembantu Tersedia)eco::response() - Kelas respons akses ( response() fungsi helper tersedia)eco::route($route, $action) - rute petaeco::router() - Kelas Akses Inti Routereco::run() - jalankan aplikasieco::service() - Layanan Loader Kelas ( service() Fungsi Pembantu Tersedia)eco::session() - Kelas Akses Sesi ( session() Fungsi Helper Tersedia)eco::set($key, $value) - Tetapkan variabel globaleco::stop() - Dengan anggun hentikan fungsi aplikasi ( stop() helper tersedia)eco::validate() - Akses Data Data Class ( validate() Fungsi Pembantu Tersedia)eco::view($template, $view_params) - Load View Template File dan kelas tampilan akses ( view() fungsi helper tersedia) Kelas inti dapat digunakan untuk menyederhanakan tugas aplikasi umum:
EcoCache - Kelas inti cache sisi serverEcoDatabase - Kelas Inti DatabaseEcoForm - HTML Form Core ClassEcoHttp - kelas inti permintaan httpEcoModel - Kelas Inti Model DatabaseEcoTable - HTML Table Core Class Kelas helper ini tersedia:
EcoBenchmark - Benchmarking HelperEcoSystemBreadcrumb - Akses dengan eco::breadcrumb() atau breadcrumb() Helper FunctionEcoSystemFilter - Akses dengan eco::filter() atau filter() fungsi helperEcoSystemSessionFlash - Access with eco::flash() atau flash() Helper FunctionEcoSystemFormat - Akses dengan eco::format() atau format() Helper FunctionEcoSystemRequest - Akses dengan eco::request() atau request() Helper FunctionEcoSystemRegistryService - Akses dengan eco::service() atau service() Helper FunctionEcoSystemSession - Akses dengan eco::session() atau session() fungsi pembantuEcoSocketClient dan EcoSocketServer - Membuat soket pembantuEcoSystemValidate - Acccess dengan eco::validate() atau validate() Helper Function Fungsi helper dapat digunakan untuk dengan cepat mengakses metode inti eco atau fungsionalitas yang berguna lainnya. Temukan dokumentasi fungsi pembantu di sini.
ECO dapat diperluas dengan memperluas kelas inti EcoSystem . Awalnya ini diatur dalam file app/com/bootstrap.php :
// set Eco access class
class eco extends Eco System {} Kelas perpanjangan dapat digunakan untuk menambahkan metode atau mengganti metode EcoSystem Overridable.
^ Kembali ke Top