PHP code example of larkframe / core
1. Go to this page and download the library: Download larkframe/core library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
larkframe / core example snippets
// server.php — Server 模式入口
RUN_TYPE_SERVER);
// shell.php — Shell 模式入口
RUN_TYPE_SHELL);
// task.php — Task 模式入口
RUN_TYPE_TASK);
// public/index.php — Web 模式入口
// config/route.php
use LarkFrame\Route;
Route::get('/hello/{name}', function ($request) {
return json(['message' => 'Hello ' . $request->route()->param('name')]);
});
namespace App\Controller;
use LarkFrame\Request;
class UserController
{
public function indexAction(Request $request)
{
return json(['users' => []]);
}
public function showAction(Request $request)
{
$id = $request->route()->param('id');
return json(['user' => ['id' => $id]]);
}
}
class AuthMiddleware implements \LarkFrame\MiddlewareInterface
{
public function process(\LarkFrame\Request $request, callable $next): \LarkFrame\Response
{
if (!$request->header('authorization')) {
return json(['error' => 'Unauthorized'], 401);
}
return $next($request);
}
}
// 配置与请求
config('app.name'); // 读取配置
request(); // 获取当前请求
input('key', $default); // 获取请求参数
// 响应
json($data); // JSON 响应
jsonp($data, 'callback'); // JSONP 响应
xml($xml); // XML 响应
redirect('/login'); // 重定向
response($body, $status); // 通用响应
view('index', $vars); // 视图响应(使用配置的引擎)
raw_view('index', $vars); // 原生 PHP 模板
twig_view('index', $vars); // Twig 模板
// 路径
run_path('sub/path'); // 程序执行目录
config_path(); // 配置目录
runtime_path('logs'); // 运行时目录
path_combine($front, $back); // 拼接路径
// 安全
escape($value); // HTML 转义(防 XSS)
clean($value); // 去除 HTML 标签并 trim
// 其他
is_phar(); // 是否在 Phar 中运行
cpu_count(); // CPU 核心数
getRealHost(true); // 获取主机名
getClientIp(); // 获取客户端 IP(支持代理头)