summonshr / requests
仅使用laravel requests
Requires
- php: ^8.2
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-16 09:38:09 UTC
README
此包受到https://github.com/spatie/laravel-route-attributes的些许启发,部分代码甚至直接复制。
为什么这个包
Laravel应用程序往往有很多控制器,但它们并不做很多事情。比如五个请求:index、create、show、edit和destroy。对于每个控制器,我们都会有五个函数,五个请求来验证这些请求。这看起来很冗余。要添加任何东西,我至少需要添加一个路由、一个控制器和一个请求。
相反,我们可以有一个接受任何内容的单个路由,它会调用一个授权、验证和处理请求的文件。
因此,发明了这个包。
不再需要控制器(如果你需要,请告诉我原因)。不再添加路由,只需通过请求深入。
一条规则统治一切。
安装
您可以通过composer安装此包
composer require summonshr/requests
用法
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Summonshr\Requests\Contracts\UniversalRequestInterface; class CreateApplication extends FormRequest implements UniversalRequestInterface { // To call on specific method const REQUEST_METHOD = 'GET'; // To call on specific action const ACTION = 'create_application'; /** * 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\ValidationRule|array|string> */ public function rules(): array { return [ // ]; } public function process() { // Process your requests here } }
在请求时,在通用路由上发送一个名为'动作'的参数。
{ "action": "create_application", // Rest of the parameters }
该包足够智能,可以自己找到方法和动作。
'store' => 'POST',
'create' => 'POST',
'edit' => 'PUT',
'destroy' => 'DELETE',
'show' => 'GET',
任何以store、create、edit、destroy、show开头的请求类将自动具有POST、POST、PUT、DELETE、SHOW。您也可以通过发布配置文件来更改这些。
如果没有指定ACTION常量,则将动作解析为请求类名称的kebab形式。
例如,CreateUserRequest将默认具有动作'create-user-request'。
如果您在Requests内部有嵌套文件夹,如User/Create.php,则动作将为'user-create',依此类推。
已经注册了一个默认处理程序,如上所述,可以通过配置文件进行更改。
Route::any('/resource', function (UniversalRequestInterface $request) { return $request->process(); });
或者,您可以自己注册一个路由,将UniversalRequestInterface作为参数。它会自动解析。
何时不使用:当你有自己的关于Laravel应用程序结构应该如何的哲学时。
我将在很多方面围绕这个进行构建。
应用程序将自动调用在Requests目录中指定的Request。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何安全相关的问题,请通过summonshr@gmail.com发送电子邮件,而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。
Laravel包模板
此包是用Laravel包模板生成的。