它是启动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使用数组会话驱动程序。