gem-partij/gemboot-lara

Gemboot Lara - 支持SMVC开发方法的Laravel包


README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock SensioLabsInsight

支持SMVC开发方法的Laravel包

它做什么

安装gemboot包之前

use App\Models\User;

class UserControllerApi extends Controller {

    // method to return all users
    public function index() {
        $status = 200;
        $message = 'Success!';
        $data = [];

        try {
            // add user data to response
            $data = User::all();
        } catch(\Exception $e) {
            // if catch error...

            // log error
            \Log::error($e->getMessage());
            \Log::error($e->getTraceAsString());

            // add error response
            $status = 500;
            $message = "Internal Server Error";
            $data = [
                'error' => $e->getMessage(),
            ];
        }

        // return response json
        return response()->json([
            'status' => $status,
            'message' => $message,
            'data' => $data,
        ], $status);
    }

}

安装gemboot包之后

use GembootResponse;
use App\Models\User;

class UserControllerApi extends Controller {

    // method to return all users
    public function index() {
        return GembootResponse::responseSuccessOrException(function() {
            return User::all();
        });
    }

}

将获得相同的响应

/* Success Response */
{
    "status": 200,
    "message": "Success!",
    "data": [
        /* all user data... */
    ]
}

/* Error Response */
{
    "status": 500, /* it could be 400 to 500 error status code */
    "message": "Error!",
    "data": [
        /* all error data... */
    ]
}

文档、安装和使用说明

有关详细的安装和使用说明,请参阅文档

支持策略

只有最新版本会获得新功能。

安装

在您的composer.json中要求gem-partij/gemboot-lara包,并更新您的依赖项

composer require gem-partij/gemboot-lara

可选:服务提供程序将自动注册。或者,您可以在config/app.php文件中手动添加服务提供程序

'providers' => [
    // ...
    \Gemboot\GembootServiceProvider::class,
];

可选:别名将自动注册。或者,您可以在config/app.php文件中手动添加gemboot别名

'aliases' => [
    // ...
    'GembootBadRequestException' => Gemboot\Exceptions\BadRequestException::class,
    'GembootForbiddenException' => Gemboot\Exceptions\ForbiddenException::class,
    'GembootNotFoundException' => Gemboot\Exceptions\NotFoundException::class,
    'GembootServerErrorException' => Gemboot\Exceptions\ServerErrorException::class,
    'GembootUnauthorizedException' => Gemboot\Exceptions\UnauthorizedException::class,

    'GembootRequest' => Gemboot\Facades\GembootRequestFacade::class,
    'GembootResponse' => Gemboot\Facades\GembootResponseFacade::class,

    'GembootController' => Gemboot\Controllers\CoreRestController::class,
    'GembootProxyController' => Gemboot\Controllers\CoreRestProxyController::class,
    'GembootResourceController' => Gemboot\Controllers\CoreRestResourceController::class,

    'GembootModel' => Gemboot\Models\CoreModel::class,

    'GembootService' => Gemboot\Services\CoreService::class,
];

Gemboot网关(附加包)

中间件

要使用Gemboot网关的所有路由,请在app/Http/Kernel.php类中$middleware属性中添加CheckToken中间件

protected $middleware = [
    // ...
    \Gemboot\Gateway\Middleware\CheckToken::class,
];

配置

默认值在config/gemboot_gw.php中设置。发布配置以将文件复制到您自己的配置

php artisan vendor:publish --tag="gemboot-gateway"

Gemboot身份验证(附加包)

中间件

要为您的路由使用Gemboot身份验证中间件,请将TokenValidatedHasRoleHasPermissionTo中间件添加到app/Http/Kernel.php类中的$routeMiddleware属性

protected $routeMiddleware = [
    // ...
    'token-validated' => \Gemboot\Middleware\TokenValidated::class,
    'role' => \Gemboot\Middleware\HasRole::class,
    'permission' => \Gemboot\Middleware\HasPermissionTo::class,
];

配置

默认值在config/gemboot_auth.php中设置。发布配置以将文件复制到您自己的配置

php artisan vendor:publish --tag="gemboot-auth"

路由

如果需要使用它,请在您的路由中添加Gemboot AuthLibrary,例如

use Illuminate\Http\Request;
use Gemboot\Libraries\AuthLibrary;

Route::middleware('api')->prefix('auth')->group(function() {
    Route::post('login', function(Request $request) {
        return (new AuthLibrary)->login($request->npp, $request->password, true);
    });

    Route::get('me', function() {
        return (new AuthLibrary)->me(true);
    });

    Route::get('validate-token', function() {
        return (new AuthLibrary)->validateToken(true);
    });

    Route::get('has-role', function(Request $request) {
        return (new AuthLibrary)->hasRole($request->role_name, true);
    });

    Route::get('has-permission-to', function(Request $request) {
        return (new AuthLibrary)->hasPermissionTo($request->permission_name, true);
    });

    Route::post('logout', function() {
        return (new AuthLibrary)->logout(true);
    });
});

Gemboot文件处理器(附加包)

配置

默认值在config/gemboot_file_handler.php中设置。发布配置以将文件复制到您自己的配置

php artisan vendor:publish --tag="gemboot-file-handler"

文件处理器使用

现在您可以使用gemboot文件处理器上传图片或文档。

use Illuminate\Http\Request;
use Gemboot\FileHandler\FileHandler;

class ExampleController extends Controller {

    public function uploadImage(Request $request) {
        $image = $request->file_image;
        $new_filename = "Gambar.jpeg";
        $save_path = "/gambar/2020";

        return (new FileHandler($image))
                ->uploadImage($new_filename, $save_path)
                ->object();
    }

}

uploadImageuploadDocument方法返回一个Illuminate\Http\Client\Response实例,该实例提供多种可能用于检查响应的方法

$response->body() : string;
$response->json($key = null) : array|mixed;
$response->object() : object;
$response->collect($key = null) : Illuminate\Support\Collection;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->redirect(): bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

更多信息:https://laravel.net.cn/docs/9.x/http-client#making-requests

测试

使用以下命令运行测试

composer test

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何安全问题,请通过电子邮件anggerpputro@gmail.com报告,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件