iqbalatma/laravel-utils

There is no license information available for the latest version (1.5.3) of this package.

1.5.3 2024-04-24 10:56 UTC

This package is auto-updated.

Last update: 2024-05-24 11:05:42 UTC


README

Laravel utils is collection of class, helper, command console for help you to efficiency your development time. Here we have command console for generate enum, abstract, interface, or trait

Config

Target dir is where generated file root path. Change this target dir location by your preference.

return [
    "target_enum_dir" => "app/Enums",
    "target_trait_dir" => "app/Traits",
    "target_abstract_dir" => "app/Contracts/Abstracts",
    "target_interface_dir" => "app/Contracts/Interfaces",
    "api_response" => [
        "payload_wrapper" => "payload",
        "meta_wrapper" => null
    ],
    "is_show_debug" => env("APP_DEBUG", false)
];

Generate File Command

You can generate enum with this utils You can also create backed enum, with string or int as type

Enum

php artisan make:enum Gender
php artisan make:enum Gender --type=string

You can generate trait with this command

Trait

php artisan make:trait HasInstitution

You can generate abstract with this command

Abstract

php artisan make:abstract BaseService

You can generate interface with this command

Interface

php artisan make:inteface IRouter

Publish Stub File

In some condition you may publish stub file and modify template.

php artisan utils:publish-stub

Formatting API Response

You can format your api response using this feature. For example :

<?php

namespace App\Http\Controllers\API\Internal\Management;

use App\Http\APIResponse;
use App\Http\Controllers\ApiController;
use App\Models\Permission;
use App\Http\Resources\Internal\Management\Permissions\PermissionResourceCollection;

class PermissionController extends ApiController
{
    /**
     * @return APIResponse
     */
    public function index(): APIResponse
    {
        $data = Permission::all();

        return $this->response(
            new PermissionResourceCollection($data),
            "Get all data permission successfully"
        );
    }
}

Description :

  • First argument is for data, you can pass paginator, string, array, resource, and null.
  • Second argument is for message, data type is string
  • Third argument is response code. You can see response code in Iqbalatma/LaravelUtils/ResponseCode
  • Forth argument is errors, mostly used for validation errors.
  • Fifth argument is for exception, mostly used for mapping execption.

The response would be

{
    "rc": "SUCCESS",
    "message": "Get all data permission successfully",
    "timestamp": "2023-10-26T23:49:47.387526Z",
    "payload": {
        "data": [
          {
            "id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab3",
            "name": "can show all user",
            "guard_name": "api"
          },
          {
            "id": "99ffccb6-b375-4cf5-9f4c-b6824fabeab4",
            "name": "can show all products",
            "guard_name": "api"
          }
      ]
    }
}

You can customize response code (rc) using third argument. You can override to add response code of this class. Just override the class and override mapHttpCode() function. For example you can add response code for ERR_NOT_FOUND()

<?php

namespace App\Services\V1;

use Iqbalatma\LaravelUtils\Interfaces\ResponseCodeInterface;
use Symfony\Component\HttpFoundation\Response;

/**
 * @method static ResponseCodeInterface ERR_NOT_FOUND()
 */
class ResponseCode extends \Iqbalatma\LaravelUtils\ResponseCode
{
    protected const ERR_NOT_FOUND = "ERR_NOT_FOUND";


    /**
     * @return void
     */
    protected function mapHttpCode(): void
    {
        $this->httpCode = match ($this->name) {
            self::ERR_NOT_FOUND => Response::HTTP_NOT_FOUND,
            default => null
        };

        if ($this->httpCode === null) {
            parent::mapHttpCode();
        }
    }
}

This is how to handle exception and customize response format:

$this->renderable(function (NotFoundHttpException $e) {
    if (request()->expectsJson()) {
        return new APIResponse(
            null,
            message: $e->getMessage(),
            responseCode: ResponseCode::ERR_NOT_FOUND(),
            exception: $e
        );
    }
});

You can change behavior of json formatting via config file.

...
"api_response" => [
    "payload_wrapper" => "payload",
    "meta_wrapper" => "meta"
],
"is_show_debug" => env("APP_DEBUG", false)
...
  • Key is_show_debug use to determine is show exception message or not. This might be cause sensitive information leaks, so do not activate this feature in production environtmen.
  • Key api_response.payload used for key of response. You can change wrapper with another wrapper or set null to not using payload wrapper.
  • Key api_response.meta_wrapper is used for wrapping data meta. When you using

Helper

ddapi()

You can use ddapi() helper function to dump api data to see data of a variable

ddapi($data);
ddapi($data, $data2);