perfect-oblivion / actions
可调用动作。不是控制器。
该软件包的规范存储库似乎已消失,因此该软件包已被冻结。
Requires
- php: ^7.1.3
- laravel/framework: 5.7.*|5.8.*|~6.0
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2022-04-29 01:03:54 UTC
README
可调用动作。不是控制器。
免责声明
PerfectOblivion命名空间下的软件包存在是为了提供一些基本功能,我不希望在每个项目中从头开始重复实现。这里没有什么突破性的东西。
可调用动作是经典MVC控制器的干净、精简的替代品。总体思路基于Paul M. Jones的“ADR - Action Domain Responder”中的“A”,请参阅ADR - Action Domain Responder。
例如,而不是使用具有“创建”、“存储”、“显示”、“编辑”等常用方法的CommentController,我们将创建具有单一职责的“动作”。可以通过构造函数或动作方法本身注入依赖项。默认情况下,我们将使用“__invoke”魔术方法,但可以进行自定义
namespace App\Http\Controllers; use App\MyDatasource; use Illuminate\Http\Request; use App\Http\Actions\Action; use App\Http\Responders\Post\IndexResponder; class PostIndex implements Action { /** * The Responder. * * @var \App\Http\Responders\Post\IndexResponder */ private $responder; /** * Construct a new PostIndex Controller. * * @param \App\Http\Responders\Post\IndexResponder $responder */ public function __construct(Responder $responder) { $this->responder = $responder; } public function __invoke(Request $request) { $data = MyDatasource::getSomeData($request); return $this->responder->withPayload($data); } }
与传统MVC风格控制器相比,一个好处是它带来的清晰度,窄类职责,更少的依赖项,以及整体组织。当与responders一起使用时,您真的可以清理“控制器”并为您的代码库带来很多清晰度。
安装
您可以通过composer安装此软件包。从您的项目目录,在您的终端中输入
composer require bright-components/actions
在Laravel > 5.6.0中,ServiceProvider将被自动检测并注册。如果您正在使用Laravel的较旧版本,请将软件包service provider添加到您的config/app.php文件中的'providers'数组
'providers' => [ //... BrightComponents\Actions\ActionServiceProvider::class, //... ];
然后运行
php artisan vendor:publish
并选择BrightComponents/Actions选项。
这将复制软件包配置(actions.php)到您的“config”文件夹。有关所有配置选项,请参阅以下内容
return [ /* |-------------------------------------------------------------------------- | Namespace |-------------------------------------------------------------------------- | | Set the namespace for the Actionss. | */ 'namespace' => 'Http\\Actions', /* |-------------------------------------------------------------------------- | Method name |-------------------------------------------------------------------------- | | Set the name for the mothod to be invoked in your actions. | */ 'method' => '__invoke', /* |-------------------------------------------------------------------------- | Duplicate Suffixes |-------------------------------------------------------------------------- | | If you have a Action suffix set in the config and try to generate a Action that also includes the suffix, | the package will recognize this duplication and rename the Action to remove the extra suffix. | This is the default behavior. To override and allow the duplication, change to false. | */ 'override_duplicate_suffix' => true, ];
使用方法
要开始使用BrightComponents/Actions,只需按照上述说明进行操作,然后根据需要生成您的Action类。例如,要生成PostIndex动作,请在上面的示例中进入以下命令到您的终端
php artisan adr:action Posts\\PostIndex
将您的逻辑放在“__invoke”方法(或配置文件中选择的名称)内。
注意:当使用动作的“__invoke”魔术方法时,您需要确保在定义路由之前动作类存在,否则您将收到一个“无效路由”异常。可调用类的路由可以定义如下
Route::get('comments', \App\Http\Actions\Comments\CommentIndex::class);
或者,您可以在路由文件的顶部导入Action命名空间,然后在路由定义中使用类的短名称
<?php use App\Http\Actions\Comments\CommentIndex; Route::get('comments', CommentIndex::class);
重要:默认情况下,我们使用生成Action类时的魔术“__invoke”方法。当使用__invoke时,您可以将路由定义为上述内容。如果您选择更改配置中的方法,您将需要以更传统的模式定义您的路由。例如,如果您选择“run”作为方法名称
Route::get('comments', 'App\Http\Actions\Comments\CommentIndex@run');
或者
Route::get('comments', \App\Http\Actions\Comments\CommentIndex::class.'@run');
此外,如果您正在使用操作的动作默认命名空间,您需要确保在您的 RouteServiceProvider 中命名空间已更新或设置为空字符串,如果您使用的是动作类的完全限定命名空间。
测试
composer test
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 clay@phpstage.com 联系,而不是使用问题跟踪器。
路线图
我们计划很快着手进行灵活性和配置的工作,以及发布一个与框架无关的包版本。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。