PHP code example of mhasnainjafri / restapikit

1. Go to this page and download the library: Download mhasnainjafri/restapikit 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/ */

    

mhasnainjafri / restapikit example snippets


use Mhasnainjafri\RestApiKit\Http\Controllers\RestController;

class BusinessController extends RestController
{
  public function store(Request $request)
    {
     $data = $this->service->create($validated);
    return $this->response($data,"Record has been saved successfully", API::CREATED);
    }

}

return $this->response($data, 'Data retrieved successfully.', API::SUCCESS);

return $this->response()
->paginate($users, 'users')
->message('Users retrieved successfully.')
->toResponse();

catch (Exception $exception) {
return $this->exception($exception, "Something went wrong", API::INTERNAL_SERVER_ERROR);
}

if ($exception instanceof ValidationException) {
return $this->response()
->errors($exception->errors())
->status(API::UNPROCESSABLE_ENTITY);
}

if ($exception instanceof UnauthorizedException) {
return $this->response()
->message('Unauthorized access')
->status(API::FORBIDDEN);
}

return $this->response()
->message('Server error')
->status(API::INTERNAL_SERVER_ERROR);

$filePath = $this->upload($file, 'uploads/documents', 'local');

$this->deleteFile($filePath, 'local');

$url = $this->fileUrl($filePath, 'local');

public function index()
{
return $this->cacheResponse('users.index', function () {
return User::all();
}, 30); // Cache for 30 minutes
}

API::success($data, 'Data retrieved successfully');
API::error('An error occurred', API::INTERNAL_SERVER_ERROR);

// Additional response helpers:
API::validationError($errors);
API::notFound('User not found');
API::cachedResponse($resource, $cacheKey);
API::paginatedCachedResponse($resource, $pageNumber);
API::clearCacheKey($cacheKey);

return API::response($data, 'Data retrieved successfully.', API::SUCCESS);

return API::response()
->paginate($users, 'users')
->message('Users retrieved successfully.')
->toResponse();

catch (Exception $exception) {
return API::exception($exception, "Something went wrong", API::INTERNAL_SERVER_ERROR);
}

if ($exception instanceof ValidationException) {
return API::validationError($exception->errors());
}

if ($exception instanceof UnauthorizedException) {
return API::error('Unauthorized access', API::FORBIDDEN);
}

return API::error('Server error', API::INTERNAL_SERVER_ERROR);

$filePath = API::upload($file, 'uploads/documents', 'local');

API::deleteFile($filePath, 'local');

$url = API::fileUrl($filePath, 'local');

public function index()
{
return API::cacheResponse('users.index', function () {
return User::all();
}, 30); // Cache for 30 minutes
}

return API::cachedResponse(User::all(), 'users.index');

API::clearCacheKey('users.index');

Route::restifyAuth();

Route::restifyAuth(['login', 'register']);

app(ActionMacroManager::class)->macro('greetUser', function ($name) {
return "Hello, {$name}!";
});
bash
php artisan RestApiKit:setup-auth
bash
php artisan vendor:publish --tag=restify-AuthControllers