定制 f9webltd/laravel-redirect-response-macros 二次开发

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

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

f9webltd/laravel-redirect-response-macros

Composer 安装命令:

composer require f9webltd/laravel-redirect-response-macros

包简介

Some useful redirect response macros for your Laravel application

README 文档

README

PHP ^8.2 Packagist Version Run Tests StyleCI Status Packagist License

Laravel Redirect Response Macros

Some super useful redirect response macros to simplify your Laravel application.

Requirements

  • PHP ^8.2
  • Laravel ^11.0 / ^12.0 / ^13.0

The package supports actively supported Laravel releases as per the official Laravel Support Policy.

Legacy Support

For legacy PHP / Laravel support, use package version 2.1.0

Installation

composer require f9webltd/laravel-redirect-response-macros

The package will automatically register itself.

Optionally publish language files by running: php artisan vendor:publish and selecting the appropriate package.

Documentation

This package allows for concise controller redirections by setting default flash data. It works as Laravels RedirectResponse class is "macroable".

For example the packages allows:

public function store(FormRequest $request)
{
    // create record ...
    return redirect()->route('posts.index')->created();
}

... instead of:

public function store(FormRequest $request)
{
    // create record ...
    return redirect()->route('posts.index')->with('success', 'The record was successfully created');
}

The former is of course much more concise and readable.

The package specifies several custom RedirectResponse macros that can be used on any of the native Laravel helpers that return the redirect response object.

The following methods are available.

success()

Flash message key: success
Pass a message string to the macros.

public function update(FormRequest $request, $id)
{
    return back()->success('Everything is great!');
}

info()

Flash message key: info
Pass a message string to the macros.

public function update(FormRequest $request, $id)
{
    return back()->info('Some information ...');
}

danger()

Flash message key: danger
Pass a message string to the macros.

public function update(FormRequest $request, $id)
{
    return back()->danger('That action just is impossible!');
}

warning()

Flash message key: warning
Pass a message string to the macros.

public function update(FormRequest $request, $id)
{
    return back()->warning('This could be risky ...');
}

There are further helper method available, that set the same type of flash data, but in a more readable manner:

created()

Flash message key: success
Default message: The record was successfully created

public function store(FormRequest $request)
{
    // create record ...
    return redirect()->route('posts.index')->created();
}

Alternatively pass a url to display an message with a link to view the created record:

public function store(FormRequest $request)
{
    // create record ...
    return redirect()->route('posts.index')->created(
      route('posts.edit', $post)
    );
}

The flashed message will now be: The record was successfully created. <a href="/posts/1/edit" class="alert-link">View inserted record</a>.

updated()

Flash message key: success
Default message: The record was successfully updated

public function update(FormRequest $requestm int $id)
{
    // update record ...
    return back()->updated();
}

To set a custom message, pass the desired text to the updated() function.

deleted()

Flash message key: success
Default message: The record was successfully deleted

public function update(Post $post)
{
    $posts->delete();

    return redirect()->route('posts.index')->deleted();
}

To set a custom message, pass the desired text to the deleted() function.

error()

Flash message key: error
Specific message text should be passed.

public function index()
{
    // code ...
    return redirect()->route('dashboard')->error('You cannot do this thing!');
}

The function can detect the presence of exception object and call getMessage() as required:

public function index()
{
    try {
        $service->run();
    } catch (Exception $e) {
        return redirect()->route('dashboard')->error($e)
    }
}

errorNotFound()

Works in the same way as the error() macro and is intended to make controllers more concise.

The default message is Sorry, the record could not be found..

authorized()

Flash message key: success
Default message: Welcome back, you have been securely logged in

A custom message can optionally be provided.

unAuthorized()

Works in the same way as the error() macro and is intended to make controllers more concise.

The default message is You do not have permission to perform that action.

IDE Autocompletion within PHPStorm

Autocompletion of "macroable" classes with PHPStorm currently difficult, which is a shame given how useful macroable classes actually are.

At present, the following process will trigger autocompletion in PHPStorm:

  • Copy resources/_ide_helper_macros.php to a location within your project to allow PHP storm to index the additional class methods
  • Optionally add _ide_helper_macros.php to your .gitignore file

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests.

Testing

composer test

Security

If you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

f9webltd/laravel-redirect-response-macros 适用场景与选型建议

f9webltd/laravel-redirect-response-macros 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.59k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2020 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「laravel redirect」 「redirect response macros」 「laravel macros」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 f9webltd/laravel-redirect-response-macros 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 f9webltd/laravel-redirect-response-macros 我们能提供哪些服务?
定制开发 / 二次开发

基于 f9webltd/laravel-redirect-response-macros 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-15