PHP code example of gem-partij / gemboot-lara

1. Go to this page and download the library: Download gem-partij/gemboot-lara 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/ */

    

gem-partij / gemboot-lara example snippets


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);
    }

}

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();
        });
    }

}

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

'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,
];

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

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

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);
    });
});

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();
    }

}

$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;
sh
php artisan vendor:publish --tag="gemboot-gateway"
sh
php artisan vendor:publish --tag="gemboot-auth"
sh
php artisan vendor:publish --tag="gemboot-file-handler"