f9webltd / laravel-redirect-response-macros
为您的Laravel应用程序提供一些有用的重定向响应宏
Requires
- php: ^8.0
- illuminate/support: ^8.12|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.4|^10.1
This package is auto-updated.
Last update: 2024-09-12 12:01:02 UTC
README
Laravel 重定向响应宏
一些超级有用的重定向响应宏,可简化您的Laravel应用程序。
要求
- PHP
^8.0
- Laravel
^8.12
,^9.0
,^10.0
或^11.0
遗留支持
对于遗留PHP / Laravel支持,请使用软件包版本 1.1.6
安装
composer require f9webltd/laravel-redirect-response-macros
该软件包将自动注册自己。
可选地通过运行: php artisan vendor:publish
并选择适当的软件包来发布语言文件。
文档
此软件包允许通过设置默认闪存数据来简洁地进行控制器重定向。它作为Laravel的 RedirectResponse
类是“可宏化”的。
例如,该软件包允许
public function store(FormRequest $request) { // create record ... return redirect()->route('posts.index')->created(); }
... 而不是
public function store(FormRequest $request) { // create record ... return redirect()->route('posts.index')->with('success', 'The record was successfully created'); }
前者当然更简洁、更易读。
该软件包指定了几个自定义 RedirectResponse
宏,可以在返回重定向响应对象的任何原生Laravel辅助函数中使用。
以下方法可用。
success()
闪存消息键: success
向宏传递消息字符串。
public function update(FormRequest $request, $id) { return back()->success('Everything is great!'); }
info()
闪存消息键: info
向宏传递消息字符串。
public function update(FormRequest $request, $id) { return back()->info('Some information ...'); }
danger()
闪存消息键: danger
向宏传递消息字符串。
public function update(FormRequest $request, $id) { return back()->danger('That action just is impossible!'); }
warning()
闪存消息键: warning
向宏传递消息字符串。
public function update(FormRequest $request, $id) { return back()->warning('This could be risky ...'); }
还有更多辅助方法可用,以更易读的方式设置相同类型的闪存数据
created()
闪存消息键: success
默认消息: 记录已成功创建
public function store(FormRequest $request) { // create record ... return redirect()->route('posts.index')->created(); }
或者传递一个URL来显示带有查看创建记录链接的消息
public function store(FormRequest $request) { // create record ... return redirect()->route('posts.index')->created( route('posts.edit', $post) ); }
现在闪存消息将是: 记录已成功创建。 <a href="/posts/1/edit" class="alert-link">查看插入的记录</a>
。
updated()
闪存消息键: success
默认消息: 记录已成功更新
public function update(FormRequest $requestm int $id) { // update record ... return back()->updated(); }
要设置自定义消息,将所需文本传递给 updated()
函数。
deleted()
闪存消息键: success
默认消息: 记录已成功删除
public function update(Post $post) { $posts->delete(); return redirect()->route('posts.index')->deleted(); }
要设置自定义消息,将所需文本传递给 deleted()
函数。
error()
闪存消息键: error
应传递特定消息文本。
public function index() { // code ... return redirect()->route('dashboard')->error('You cannot do this thing!'); }
函数可以检测异常对象的存在,并按需调用 getMessage()
public function index() { try { $service->run(); } catch (Exception $e) { return redirect()->route('dashboard')->error($e) } }
errorNotFound()
与 error()
宏的工作方式相同,旨在使控制器更简洁。
默认消息是 抱歉,找不到该记录。
authorized()
闪存消息键: success
默认消息: 欢迎回来,您已安全登录
可以可选地提供自定义消息。
unAuthorized()
与 error()
宏的工作方式相同,旨在使控制器更简洁。
默认消息是 您没有权限执行该操作
。
PHPStorm中的IDE自动完成
由于可宏化类实际上非常有用,因此PHPStorm中“可宏化”类的自动完成目前很困难,这真是太遗憾了。
目前,以下过程将在PHPStorm中触发自动完成
- 将resources/_ide_helper_macros.php 复制到项目中的某个位置,以便PHP storm索引额外的类方法
- 可选地将
_ide_helper_macros.php
添加到您的.gitignore
文件中
贡献
欢迎任何想法。请随时提交任何问题或拉取请求。
测试
composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件rob@f9web.co.uk联系我们,而不是使用问题跟踪器。
鸣谢
- Rob Allport 为 F9 Web Ltd. 贡献。
许可
MIT 许可证(MIT)。请参阅许可文件以获取更多信息。