它是啟動PHP項目的好選擇,可用於Web和API開發。啟動一個項目只需要一分鐘。它具有超級高性能和非常易於使用的開發體驗。沒有復雜的概念,因此它的學習曲線最低。
composer create-project amazephp/amazephp
或使用git克隆:
git clone https://github.com/w3yyb/AmazePHP.git
cd AmazePHP
composer install
cd public/
php -S localhost:9080 server.php
打開http:// localhost:9080在您的瀏覽器中。
PHP 8.1+
該應用程序目錄包含您應用程序的核心代碼。我們將盡快詳細介紹該目錄;但是,您的應用程序中幾乎所有類都將在此目錄中。
顧名思義,配置目錄包含您應用程序的所有配置文件。包括路由配置文件。
助手在其中起作用。
框架核心目錄包括一些LIB類。您可以將類文件放入其中。
公共目錄包含index.php文件,這是輸入應用程序並配置自動加載的所有請求的輸入點。該目錄還包含您的資產,例如圖像,JavaScript和CSS。
緩存目錄包含您的緩存文件,包括日誌文件。
模板目錄包含您的HTML模板文件。
config('app'); //will read config/app.php, app.php return an array.
config('app.url')// == config('app')['url'];
// Retrieve a default value if the configuration value does not exist...
$value = config('app.timezone', 'Asia/Seoul');
要在運行時設置配置值,請將數組傳遞到配置函數:
config(['app.timezone' => 'America/Chicago']);
$value = cache('key');
cache(['key' => 'value'], 10);// Expires after 10 seconds
模板引擎使用BladeOne ,類似於Laravel blade的模板引擎,請單擊此處https://github.com/eftec/bladeone/wiki/wiki/bladeone-manual視圖bladeone手冊。
echo view('greeting', ['name' => 'James']);
第一個參數是模板名稱,即template/greeting.blade.php ,第二個參數是傳遞到模板中的變量。
env('key');
env('key','default');
傳遞給ENV函數的第二個值是“默認值”。如果給定密鑰不存在環境變量,則將返回此值。
logger('some msg');//error log
logger('some msg','warning'); //warning log | support:emergency ,alert ,critical ,error ,warning ,notice ,info ,debug
請參閱config/route.php
[
['GET'],
'/',
[AppControllersIndex::class, 'index'],
'routename',
'middleware'=>[AppMiddlewarea2Middleware::class,AppMiddlewareb2Middleware::class],
],
第一行是HTTP請求方法,該方法支持Head,Get,Post,Put,Patch,Patch,Delete。 ['POST,GET']表示支持兩個帖子和獲取。 ['*']表示支持所有HTTP方法。
第二行代表路徑,例如/users/{uid}/posts/[{pid}][/] :在curly braces中是變量參數,括號中的可選參數,即尚未在URL中傳遞的參數, [/]用於刪除後斜線。
第三行指示PHP回調,對類方法的支持,類靜態方法,匿名功能,功能等。
第四行是可選的,並指示指定路由的名稱。
中間件密鑰是可選的寄存器路由中間件。
Amazphp 請求類提供了一種面向對象的方法,可以與您的應用程序處理的當前HTTP請求進行交互,並檢索與請求一起提交的輸入,cookie和文件。
$input = request()->all();
$name = request()->input('name');
$value = request()->cookie('name');
$value = request()->header('X-Header-Name');
$method = request()->method();
request()->host();
$url = request()->url();
$urlWithQueryString = request()->fullUrl();
$uri = request()->path();
if (request()->is('admin/*')) {
// ...
}
$input = request()->only(['username', 'password']);
$input = request()->except(['credit_card']);
$file = request()->file('upload');
if ($file && $file->isValid()) {
$file->move(PUBLIC_PATH.'/myfile.'.$file->getUploadExtension());
return json(['code' => 0, 'msg' => 'upload success']);
}
更多用法請參見Amazpephp/src/request.php。
出於績效原因,AmazphePHP不提供求助類別。使用header()funcion和Echo或返回控制器或中間件中的響應。
您不得希望使用“控制器”類將所有請求處理邏輯定義為路由文件中的關閉。控制器可以將相關請求處理邏輯分組為單個類。例如, UserController類可能會處理與用戶相關的所有傳入請求,包括顯示,創建,更新和刪除用戶。默認情況下,控制器存儲在app/Controllers目錄中。
在app/Controllers DIR中,您可以編寫一些控制器,例如:
<?php
namespace AppControllers;
class Index
{
public function index()
{
echo 'Hello AmazePHP!';
}
}
寫了一個控制器類和方法後,您可以定義通往控制器方法的路由:
[
['GET'],
'/',
[AppControllersIndex::class, 'index']
],
當傳入請求與指定的路由URI匹配時,將調用App Controllers Index類上的索引方法,並將路由參數傳遞給該方法。
中間件,也稱為HTTP中間件,主要用於修改或過濾HTTP請求或響應。這些中間件的所有中間軟件都位於App/Middleware目錄中。
中間件被分為中間件之前和中間件之後。之前,中間件主要用於修改HTTP請求。在中間件主要用於修改HTTP響應之後。
Request->Before middleware->Actual action->After middleware->Response
中間件之前和中間件之後的主要區別是執行代碼的位置。在應用程序/中間件目錄中:
創建例如bmiddleware.php
<?php
namespace AppMiddleware;
use AmazePHPMiddlewareInterface;
class bMiddleware implements MiddlewareInterface {
public function process($object, Closure $next,...$params)
{
//Perform some logic here
return $next($object);
}
}
?>
創建例如amiddleware.php
<?php
namespace AppMiddleware;
use AmazePHPMiddlewareInterface;
class aMiddleware implements MiddlewareInterface {
public function process($object, Closure $next,...$params)
{
$response = $next($object);
//Perform some logic here
return $response;
}
}
?>
在config/middleware.php中,編寫以下內容:
return [
AppMiddlewareaMiddleware::class,
AppMiddlewarebMiddleware::class,
];
請參閱路由。
FAçade為框架的核心類庫的(動態)類提供了一個靜態調用接口。
在App Controllers Index Controller中:
<?php
namespace AppControllers;
use AmazePHPFacadeRequest;
class Index
{
public function index()
{
echo Request::url();//call Request.php url method statically. Same as calling Request->url().
}
}
系統的所有立面都放置在Amazphp/SRC/立面目錄中。
容器是管理班級依賴和執行依賴注入的強大工具。
例如:
<?php
namespace AppControllers;
use AmazePHPRequest;
class Foo
{
public function bar(Request $request, $id)
{
echo $request->url();
}
}
?>
欄方法取決於請求類。您可以將請求$請求放入bar參數中。幀件將自動調用請求類,因此您可以使用請求類方法:例如$request->url() 。
支持使用依賴注射使用的方案包括(但不限於):
$response= httpGet('http://httpbin.org/get');
$response= httpGet('http://httpbin.org/get',['headername'=>'headervalue']);
$response= httpHead('http://httpbin.org/get',['headername'=>'headervalue']);
$response= httpDelete('http://httpbin.org/delete',['headername'=>'headervalue']);
$response= httpPost('http://httpbin.org/post',['senddataname'=>'senddatavalue']);
$response= httpPut('http://httpbin.org/put',['senddataname'=>'senddatavalue']);
$response= httpPatch('http://httpbin.org/patch',['senddataname'=>'senddatavalue']);
$響應是一個包含status_code,標頭和車身數據的數組。
功能參數如下:
httpGet($url,$header = [])
httpHead($url,$header = [])
httpDelete($url,$header = [])
httpPost($url, $data, $isJson = true,$method='POST',$header = [])
httpPut($url, $data, $isJson = true,$method='PUT',$header = [])
httpPatch($url, $data, $isJson = true,$method='PATCH',$header = [])
默認情況下,會話是關閉的,如果要打開,請將.ENV文件中的session_enable更改為true。
session(["name" => "value"]);
$value = session('name')
$value = request()->cookie('name');
cookie('name','value',86400); // 86400 seconds
數據庫組件使用PdoOne ,PDOONE是PHP和PDO的數據庫訪問對象包裝器。單擊https://github.com/eftec/pdoone,以查看如何使用它。
以下是如何以簡單方式使用它的示例。
選擇:
$results = db()->select("*")->from('users')->where("name like '%test%'")->toList();
print_r($results);
使用RAW SQL:
$sql='select * from users where id=1';
$pdoStatement=db()->runRawQuery($sql,[],false); // [] are the parameters
print_r($pdoStatement->fetchAll());
插入:
db()->insert("users"
,['name','email','password']
,['kevin','[email protected]','123456']);
更新:
db()->update("users"
,['name'=>'Captain-Crunch','email'=>'[email protected]'] // set
,['id'=>6]); // where
刪除:
db()->delete("users"
,['id'=>6]); // where
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
echo url("/posts/{$post->id}"); // http://example.com/posts/1
[
['GET'],
'/hello/{id}/foo/{sid}',
[new AppFoo, 'bar'],
'nameroute1'//Named Route
],
echo route('nameroute1', ['id' => 1, 'sid' => 2]);
// http://example.com/hello/1/foo/2
如果要使用它,請首先啟用會話。
跨站點請求偽造是一種惡意利用,代表身份驗證的用戶執行未經授權的命令。值得慶幸的是,Amazphpp使保護您的應用程序免受跨站點請求偽造(CSRF)攻擊變得容易。
AmazPhp P會自動為應用程序管理的每個活動用戶會話生成CSRF“令牌”。該令牌用於驗證身份驗證的用戶是實際向應用程序提出請求的人。由於此令牌存儲在用戶的會話中,並且每次將會話再生時都會更改,因此惡意應用程序無法訪問它。
可以通過csrf_token輔助功能訪問當前會話的CSRF令牌:
$token = csrf_token();
每當您在應用程序中定義“帖子”,“ put”,“ patch”或“ delete” html表單時,您應該在表單中包含一個隱藏的csrf _token字段,以便CSRF Protection Protection Middleware可以驗證請求。為了方便起見,您可以使用@csrf Blade指令生成隱藏的令牌輸入字段:
<form method="POST" action="/profile">
@csrf
<!-- Equivalent to... -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
</form>
除了檢查CSRF令牌作為後參數外, libVerifyCsrfToken還將檢查X-CSRF-TOKEN請求標頭。例如,您可以將令牌存儲在HTML meta標籤中:
<meta name="csrf-token" content="{{ csrf_token() }}">
然後,您可以指示像jQuery這樣的庫將令牌自動添加到所有請求標題中。這為您的基於AJAX的應用程序提供了簡單,方便的CSRF保護,並使用Legacy JavaScript技術:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
AmphePhP將當前的CSRF令牌存儲在加密的XSRF-TOKEN cookie中,該cookie包含在框架生成的每個響應中。您可以使用cookie值來設置X-XSRF-TOKEN請求標頭。
該cookie主要作為開發人員的便利性發送,因為某些JavaScript框架和庫(例如Angular和Axios)會在同一孔請求下自動將其值放在X-XSRF-TOKEN標頭中。
./phpunit --bootstrap vendor/autoload.php tests
./phpunit --bootstrap vendor/autoload.php tests --display-warnings
./phpunit --bootstrap vendor/autoload.php tests --display-deprecations
Amphephp在hello world Benchmark中比Laravel快9倍。
Laravel:2900 RPS。
Amazphpp:23000 RPS。
兩者都打開調試,Laravel使用數組會話驅動程序。