Download the PHP package haxneeraj/laravel-api-kit without Composer

On this page you can find all versions of the php package haxneeraj/laravel-api-kit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-api-kit

LaravelAPIKit

LaravelAPIKit is a package designed to enhance your Laravel application's API capabilities. It provides standardized JSON responses, streamlined exception handling, customizable data transformers, and custom request validation. The package also includes helpful artisan commands to automate the creation of transformers and custom requests, making API development faster and more efficient.

Features

Installation

To install the LaravelAPIKit package in your Laravel project, follow these steps:

  1. Install via Composer:

  2. Add the service provider to the config/app.php file (this step may be optional if you're using Laravel's package auto-discovery):

For Laravel 10 and below:

'providers' => [
    // Other service providers...
    Haxneeraj\LaravelAPIKit\LaravelAPIKitServiceProvider::class,
],

For Laravel 11: Add the service provider to the bootstrap/providers.php file (this step may be optional if you're using Laravel's package auto-discovery):

return [
    App\Providers\AppServiceProvider::class,
    Haxneeraj\LaravelAPIKit\LaravelAPIKitServiceProvider::class,
];

Configuration

1. Publish the Configuration File

LaravelAPIKit provides a configuration file that allows you to customize how the package behaves. To publish the configuration file, run the following command:

php artisan vendor:publish --provider="Haxneeraj\LaravelAPIKit\LaravelAPIKitServiceProvider" --tag=config

This will publish a laravel-api-kit.php file in your config directory, where you can set things like the exception handling mode (e.g., dev_mode = true/false/'dev'). Customize the configuration file to suit your needs.

You can set the exception handling mode in the .env file so that other parts of the package use the same mode:

HAX_DEBUG_MODE = true/false/'dev'

2. Publish Translations

LaravelAPIKit also comes with translatable strings that you can modify. To publish these translations, run:

php artisan vendor:publish --provider="Haxneeraj\LaravelAPIKit\LaravelAPIKitServiceProvider" --tag=lang

You can now customize the translation files in resources/lang/haxneeraj/laravel-api-kit/.

Usage

__('LaravelAPIKit::message.not_found')

1. JSON Standardized Response Trait

Use the APIResponseTrait in your controllers to standardize the API responses. This trait provides methods for returning success or error responses, including custom messages and HTTP status codes.

Example:

use Haxneeraj\LaravelAPIKit\Traits\APIResponseTrait;

class ExampleController extends Controller
{
    use APIResponseTrait;

    public function index()
    {
        return $this->response(true, 'Data fetched successfully', ['key' => 'value']);
    }
}

Methods

response($status = true, $message = '', $data = [], $errors = [], $code = Response::HTTP_OK): JsonResponse

responseCreated(?string $message = null, array $data = []): JsonResponse

responseDeleted(?string $message = null): JsonResponse

responseNotFound(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

responseBadRequest(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

responseUnAuthorized(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

responseConflictError(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

responseUnprocessable(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

responseUnAuthenticated(?string $errorTitle = null, mixed $errorDetails = null): JsonResponse

Example Usage

2. Exception Handling

The package provides a dedicated exception handler that converts common exceptions (such as model not found, validation errors, etc.) into standardized JSON responses. Extend our exception handler to customize the behavior or add additional exception handling logic.

Exception Handler

use Haxneeraj\LaravelAPIKit\Exceptions\ApiException;

class Handler extends ApiException
{
    try
    {
        //
    }
    catch(\Exception $e)
    {
        return $this->handleException($e);
    }
}

Note: The ApiException class used the APIResponseTrait Trait so you don't need to import it.

Or You Can Make a Common Class for All API Controllers and Extend it.

use Haxneeraj\LaravelAPIKit\Exceptions\ApiException;

class ApiController extends ApiException
{
    //
}

and in your controllers extends it.

use App\Http\Controllers\ApiController;

class ExampleController extends ApiController
{
    //
}

Other way to use it as object.

use Haxneeraj\LaravelAPIKit\Exceptions\ApiException;

class ExampleController extends Controller
{
    public function __construct()
    {
        $this->apiException = new ApiException();
    }

    public function index()
    {
        try
        {
            //
        }
        catch(\Exception $e)
        {
            return $this->apiException->handleException($e);
            // or you can call the method statically. - return ApiException::handleException($e);
        }
    }
}

Note: In this case, if you want to use the APIResponseTrait Trait in your controller, you need to import it.

3. Data Transformer

The package provides a data transformer that simplifies the transformation of models or other data structures for API responses. Use the following artisan command to create a new transformer class:

php artisan make:transformer UserTransformer

//Or want to give a directory structure then
php artisan make:transformer User/Usertransformer

The transformer class will be generated in the app/Transformers directory.

Transformer Class

use Haxneeraj\LaravelAPIKit\Transformers\LaravelAPIKitTransformer;

class UserTransformer extends LaravelAPIKitTransformer
{
    public function model($user)
    {
        // you can use $user->id, $user->name, $user->email, etc.
        return [
            //'id' => $user->id,
            //'name' => $user->name,
            //'email' => $user->email,
        ];
    }
}

Use Case

use App\Transformers\UserTransformer;
use App\Http\Controllers\APIController;

class UserController extends APIController
{
    public $transformer;

    public function __construct(UserTransformer $transformer)
    {
        $this->transformer = $transformer;
    }

    public function index()
    {
        $users = User::all();
        return $this->response(true, 'Users fetched successfully', $this->transformer->transform($users));
    }
}

4. Custom Requests

The package provides a custom request class that allows you to create custom request validation logic to handle incoming API requests with ease. Use the following artisan command to create a new request class:

php artisan make:request ExampleRequest

// or use the directory structure
php artisan make:request Example/ExampleRequest

The Request class will be generated in the app/Http/Requests directory.

ExampleRequest Class

use Haxneeraj\LaravelAPIKit\Http\Requests\LaravelApiKitRequest;

class ExampleRequest extends LaravelApiKitRequest
{
    /**
    * Determine if the user is authorized to make this request.
    */
    public function authorize(): bool
    {
        return true;
    }

    /**
    * Get the validation rules that apply to the request.
    *
    * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
    */
    public function rules(): array
    {
        return [
            //
        ];
    }
}

License

The LaravelAPIKit package is open-sourced software licensed under the MIT license.

Contributing

Contributions are welcome and greatly appreciated. Please check out the contribution guidelines for more information.

Credits

Author: Neeraj Saini Email: [email protected] GitHub: https://github.com/haxneeraj/ LinkedIn: https://www.linkedin.com/in/hax-neeraj/

Twitter: https://x.com/hax_neeraj/


All versions of laravel-api-kit with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package haxneeraj/laravel-api-kit contains the following files

Loading the files please wait ....