PHP code example of muzk6 / jdi
1. Go to this page and download the library: Download muzk6/jdi 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/ */
muzk6 / jdi example snippets
// 框架初始化
\JDI\App::init();
// 注册路由
route_get('/', function () {
return 'Just Do It!';
});
// 分发路由
route_dispatch();
route_middleware(function () {
echo '中间件a1'; // 这里为了测试才用 echo,实际开发时只用于处理逻辑而不需要打印
});
route_middleware(function () {
echo '中间件a2';
});
route_group(function () {
route_middleware(function () {
echo '中间件b1';
});
route_get('/mid', function () {
return 'Just Do It!';
});
});
route_middleware(function () {
echo '中间件a3';
});
// 不指定参数三,用默认方式
route_post('/xhr', function () {
panic();
});
// 指定参数三,自定义异常处理
route_post('/doc', function () {
panic('doc error');
}, function (Exception $exception) {
if ($exception instanceof AppException) {
// 只提示业务异常
alert($exception->getMessage());
} else {
// 非业务异常不提示
back();
}
});
route_get('/idnex', function() {
return [input('get.da:i'), svc_auth()->isLogin()];
});
route_simulate('/idnex', ['da' => 10], 1010); // 注意在这之前不能调用 route_dispatch()
$first_name = input('first_name');
$last_name = input('last_name');
var_dump($first_name, $last_name);
$request = request();
var_dump($request);
$first_name = input('first_name');
$last_name = validate('last_name')->
validate('last_name')->var_dump($request);
validate('first_name')->/ 以串联短路方式验证
var_dump($request);
validate('first_name')->); // 以并联方式验证
function svc_foo()
{
return \JDI\App::singleton(__FUNCTION__, function () {
return new Foo();
});
}
\JDI\App::set('svc_foo', function () {
return new Bar();
});
\JDI\App::unset('svc_foo');
log_push('test', ['user_id' => 123, '这里写日志']);
log_push('test', ['user_id' => 123, '另一处又写日志']);
svc_log()->setExtraData('user_id', 123);
log_push('test', ['这里写日志']);
log_push('test', ['另一处又写日志']);
log_push('test', ['这里写日志']);
log_push('test', ['另一处又写日志']);
svc_log()->setExtraData('user_id', 123);
svc_log()->autoFlush(false); // 关闭自动刷写
svc_log()->flush(); // 手动刷写
svc_auth()->login(1010); // 登录 ID 为 1010
svc_auth()->getUserId(); // 1010
svc_auth()->isLogin(); // true
svc_auth()->logout(); // 退出登录