PHP code example of muzk6 / sparrow

1. Go to this page and download the library: Download muzk6/sparrow 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 / sparrow example snippets


route_middleware(function () {
    echo '中间件a1';
});

route_middleware(function () {
    echo '中间件a2';
});

route_group(function () {
    route_middleware(function () {
        echo '中间件b1';
    });

    route_get('/', function () {
        return 'Just Do It!';
    });
});

route_middleware(function () {
    echo '中间件a3';
});

app(Router::class)->setStatus404Handler(function () {
    return '自定义404页面'; // return view('...')
});

$firstName = input('post.first_name');
$lastName = input('last_name');
var_dump($firstName, $lastName);exit;

input('post.first_name');
input('last_name');
$request = request();
var_dump($request);exit;

$firstName = input('post.first_name');
$lastName = validate('last_name')->

input('post.first_name');
validate('last_name')->

validate('post.first_name')->red()->setTitle('名字');
$request = request(); // 以串联短路方式验证

validate('post.first_name')->red()->setTitle('名字');
$request = request(true); // 以并联方式验证

inject(function (\Core\Queue $queue) {
    //todo...
});

return ['foo' => 1]; // 只能返回消息体 d
return api_success('', 0, ['foo' => 1]); // 一般用于方便返回纯 m, 例如 api_success('我是成功消息');
return api_json(true, ['foo' => 1]);

panic('我是失败消息', ['foo' => 1]); // 直接抛出异常,不用 return; 另一种便捷的用法是 panic(10001000);
return api_error('我是失败消息', 0, ['foo' => 1]); // 可自由指定错误码
return api_json(false, ['foo' => 1]);

app(\Core\Auth::class)->login(1010); // 登录 ID 为 1010
app(\Core\Auth::class)->getUserId(); // 1010
app(\Core\Auth::class)->isLogin(); // true
app(\Core\Auth::class)->logout(); // 退出登录