victor-falcon/laravel-task
Composer 安装命令:
composer require victor-falcon/laravel-task
包简介
A simple laravel task trigger
README 文档
README
🇪🇸 Documentación en español aqui
Table of content:
Installation
Install via composer
# PHP >= 8 composer require victor-falcon/laravel-task # Previous PHP versions composer require victor-falcon/laravel-task:1.1.4
Usage
1. Basic usage
Create a simple task using:
artisan task:make Shop/CreateUserShop
You can pass Shop/CreateUserShop to create the class in a sub-folder or just the task name. The default path is
app/Tasks.
<?php declare(strict_types=1); namespace App\Tasks\Shop; use VictorFalcon\LaravelTask\Task; use VictorFalcon\LaravelTask\Taskable; final class CreateUserShop implements Task { use Taskable; private User $user; public function __construct(User $user) { $this->user = $user; } public function handle(ShopCreator $creator): Shop { // Create your shop } }
and trigger it:
$shop = CreateUserShop::trigger($user);
2. With validation
If you want, you can pass validated data to your tasks using the Laravel validator. For example, If you need to validate a user creation you can do something like this.
final class CreateUser implements Task { public function rules(): array { return [ 'name' => 'required|string|min:5', 'email' => 'required|email|unique:users,email', ]; } public function handle(): User { return User::create($this->data); } }
And then you can trigger your task with extra data using:
CreateUser::trigger()->withValid([ 'name' => 'Víctor Falcón', 'email' => 'hi@victorfalcon.es', ]);
You can customize the messages with the method messages(): array in your task or add custom attributes with customAttributes(): array.
If you want to customize the errors bag name of the validator just define the string $errorBag property in your class.
3. With authorization
Sometimes you need to check if the user triggering the task is authorized or not. You can do that by adding a simple authorize(): bool method to your task. If this method returns false an AuthorizationException will thrown on before execution.
public function authorize(): bool { return $this->user()->can('create', Product::class); }
In any task you can access to the current logged user with $this->user() or, if you want, you can pass a user object by doing:
CreateProduct::trigger()->by($user);
4. Recover response
By default, each task is executed without returning any value. So if you want to recover the result of a task you must call the result() method.
// This will return the result of the `handle` method inside our task $product = CreateProduct::trigger()->withValid($data)->result();
Generate IDE Help
In order to make more easy to write and use your task you can generate a _ide_helper_tasks.php file automatically with the artisan task:ide-help command.
Update config
If you want, you can publish the package config to customize, for example, where do you want you task to be store at:
artisan vendor:publish --tag=laravel-task
Credits
victor-falcon/laravel-task 适用场景与选型建议
victor-falcon/laravel-task 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 272 次下载、GitHub Stars 达 17, 最近一次更新时间为 2020 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「task」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 victor-falcon/laravel-task 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 victor-falcon/laravel-task 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 victor-falcon/laravel-task 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expose the DDev executable commands to the Robo task runner.
Task system for APPUI
Alfabank REST API integration
AsyncTask enables proper and easy use of the thread. This class allows to perform background operations and publish results on the thread without having to manipulate threads and/or handlers.
This is a PHP Framework base on Swoole
Provides a command line tasks to operate with APC (APCu) cache from the console
统计信息
- 总下载量: 272
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-12