trabdlkarim/lararestler
Composer 安装命令:
composer require trabdlkarim/lararestler
包简介
A package for having Restler API in your Laravel application.
README 文档
README
Restler for Laravel
Lararestler is a simple package that lets you to take advantage of all the features from both Restler 5 and Laravel as your API backend. With this package you can seamlessly integrate Restler into your existing laravel application without any hassle.
Installation
You can easily install this package with conmposer.
composer require trabdlkarim/lararestler
Warning
You should note that this package works actually with Laravel framework ^12.x.
After the installation, you must publish the package assets for customization. Run the following command:
php artisan vendor:publish --tag=lararestler-config --tag=lararestler-assets
The following files should be generated if everything works fine:
config/lararestler.phpresources/views/vendor/lararestler/explorer.blade.phppublic/vendor/lararestler/*
Now, feel free to change or modifiy any of these files as needed, to adapt them to your project.
Usage
Before starting writting your API, make sure you've properly set the API resource namespace in config/lararestler.php.
The default namespace is App\Http\Resources.
Add Resources to API
Create a new class and extend Mirak\Lararestler\Http\Resources\Resource class.
Add all the needed private and public methods as you would do with Restler.
Let's define a basic API class called Users as an example.
<?php namespace App\Http\Resources; use App\Models\User; use Mirak\Lararestler\Attributes\QueryParam as QParam; use Mirak\Lararestler\Exceptions\RestException; use Mirak\Lararestler\Http\Resources\Resource; class Users extends Resource { /** * Retrieve users * * @param int $id User ID * @param string $email Email Address * @param string $name Name * @return App\Http\Responses\UserResponse */ public function index(#[QParam('id')] ?int $id = 0, #[QParam('email')] ?string $email = '', #[QParam('name')] ?string $name = '') { $builder = User::select('*'); if ($id) { $builder = $builder->where('id', $id); } if ($email) { $builder = $builder->where('email', $email); } if ($name) { $builder = $builder->where('name', 'like', '%' . $name . '%'); } return $builder->get(); } /** * Retrieve a user info * * Retrieve a given user details from storage * * @param int $id User ID * @return array * @url GET /{id} * @throws RestException */ public function get(int $id) { $user = User::find($id); if (!$user) throw new RestException(404, "User #{$id} does not exist."); return $user; } }
Note that we make use of Mirak\Lararestler\Attributes\QueryParam contextual attribute to caputure relevant query parameters from the current request.
There is also another attribute Mirak\Lararestler\Attributes\RequestBody, useful when you want to capture all request body as an array.
Afterwards, you need to add your newly created resource class to the API resources array in config/lararestler.php file as shown below.
<?php return [ "name" => "REST API", "version" => 1, "path_prefix" => "api", "middleware" => ["api"], "allowed_envs" => ['local'], "namespace" => "App\\Http\\Resources", "resources" => [ "users" => "Users", ], ];
That's it, you're done! Visit the Explorer at {APP_URL}/api/explorer to play with the API.
Request Models
Your request models should extend Mirak\Lararestler\Http\Requests\Model class as follows:
<?php namespace App\Http\Requests; use Mirak\Lararestler\Http\Requests\Model; class UserRequest extends Model { /** * @var string Name {@required true} */ public $name; /** * @var string Email Address {@required true} */ public $email; }
Then, you could inject them into your resource class methods and use them as an instance of Laravel Illuminate\Http\Request class.
Check the example below.
<?php namespace App\Http\Resources; use App\Http\Requests\UserRequest; use App\Models\User; use Mirak\Lararestler\Attributes\QueryParam as QParam; use Mirak\Lararestler\Exceptions\RestException; use Mirak\Lararestler\Http\Resources\Resource; class Users extends Resource{ /** * Create a new user * * @param UserRequest $postRequest * @return App\App\Http\Responses\UserResponse */ public function post(UserRequest $postRequest) { $v = $postRequest->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'max:255'], 'phone' => ['nullable', 'string', 'max:255'], 'role' => ['nullable', 'string', 'max:255'], 'location' => ['nullable', 'string', 'max:255'], ]); $user = User::create([ 'name' => $v['name'], 'email' => $v['email'], 'phone' => $v['phone'], 'role' => $v['role'], 'location' => $v['location'], 'password' => 'pass123456', ]); return $user; } }
Protected API Routes
To protect a route in your resource class, simply add the @middleware annotation, followed by the name of the middleware, to the method documentation.
/** * @param Illuminate\Http\Request $request * @url POST /test * @middleware auth:web,admin */ public function postTest(Request $request){ // }
Contributing
Thank you for considering contributing to this package! You should take a look at the contribution guide here.
Code of Conduct
In order to ensure that the Lararestler community is welcoming to all, please review and abide by the Code of Conduct.
Security
If you discover a security vulnerability within this package, please send me an e-mail via mail@trabdlkarim.me. All security vulnerabilities will be promptly addressed.
License
This is an open-sourced software licensed under the MIT license.
trabdlkarim/lararestler 适用场景与选型建议
trabdlkarim/lararestler 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「api」 「laravel」 「rest-api」 「Restler」 「restful-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 trabdlkarim/lararestler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 trabdlkarim/lararestler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 trabdlkarim/lararestler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Simple ASCII output of array data
Alfabank REST API integration
Package for view storage in laravel
User Approval Laravel Package
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-17
