定制 olegstan/laravel-rest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

olegstan/laravel-rest

最新稳定版本:1.1.53

Composer 安装命令:

composer require olegstan/laravel-rest

包简介

Powerful REST for the Laravel framework.

README 文档

README

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

Example request handling:

const form = new FormData();
form.append('data[user_id]', 10);
form.append('data[first_name]', 'Andrey');
form.append('data[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 olegstan@inbox.ru.

统计信息

  • 总下载量: 886
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-27