Skip to content

olegstan/laravel-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

olegstan/laravel-rest Package Documentation

Overview

The olegstan/laravel-rest package offers a streamlined approach to building RESTful APIs with Laravel, emphasizing convention over configuration. It automatically sets up API routes and organizes the application structure to enhance the development of REST APIs. This document guides you through the installation, setup, and basic usage of the package.

Installation

Begin by installing the package through Composer:

composer require olegstan/laravel-rest

This command adds the olegstan/laravel-rest package to your project, enabling its features and functionalities.

Associated Package

For optimal use of olegstan/laravel-rest, install the related laravel-request package, which complements the main package by handling request validations and more. Find it at:

https://github.com/olegstan/laravel-request

Configuration

After installation, publish the package's assets and configurations to your project:

php artisan vendor:publish --provider="LaravelRest\LaravelRestServiceProvider"

This command copies necessary files into your project, setting the stage for the API's structure and behavior.

Automatic API Routing

With the package published, API routes are automatically configured. The routing convention follows the pattern:

/api/v1/call/{controllerName}/{functionName}

This pattern facilitates direct access to controller methods via API endpoints. For instance, to access the postStore method in ClientController, you would use:

  • URL: /api/v1/call/client/store
  • Method: POST

Project Structure

The package organizes your Laravel project in a manner conducive to API development, as follows:

/App
    /Api
        /Controllers
            /Common - Controllers without authentication requirements
            /AnyRole - Controllers for specific role-based access
        /Helpers - Utility functions and classes
        /Requests - Custom request validation classes
        /Transformers
            /Base - Common data transformers
            /AnyRole - Role-specific data transformers

Controllers and Requests

Controllers use the StartRequestInterface for handling requests, ensuring a standard approach to request processing across your application.

Example request handling:

const form = new FormData();
form.append('user_id', 10);
form.append('first_name', 'Andrey');
form.append('last_name', 'Kirov');

const requestOptions = {
  method: "POST",
  body: form
};

let url = SERVER_API + '/api/v1/call/client/store';

await fetch(url, requestOptions)
  .then(response => response.json())
  .then(json => { /* Handle the JSON response */ })
  .catch(error => { /* Handle errors */ });

Handling Data in Controllers

Data sent to an endpoint, such as in the example above, is easily retrievable in the controller:

class ClientController extends BaseController
{
    public function postStore(Request $request)
    {
        $userId = $request->input('user_id');
        $firstName = $request->input('first_name');
        $lastName = $request->input('last_name');

        // Implement your logic here

        return $this->response()->success();
    }
}

Support

For questions or further assistance with the olegstan/laravel-rest package, please reach out to [email protected].

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages