Actions 本文未发布 发布文章

未匹配的标注

Definition & Principles

Read the section in the Porto SAP Documentation (#Actions).

Rules

  • All Actions MUST extend App\Ship\Parents\Actions\Action.

Folder Structure

 - app
    - Containers
        - {container-name}
            - Actions
                - CreateUserAction.php
                - DeleteUserAction.php
                - ...

Code Sample

Delete User Action:

<?php

namespace App\Containers\User\Actions;

use Apiato\Core\Foundation\Facades\Apiato;
use App\Containers\User\Models\User;
use App\Ship\Parents\Actions\Action;

class CreateAdminAction extends Action
{

    /**
     * @param string $email
     * @param string $password
     * @param string $name
     * @param bool   $isClient
     *
     * @return  \App\Containers\User\Models\User
     */
    public function run(string $email, string $password, string $name, bool $isClient = false): User
    {
        $admin = Apiato::call('User@CreateUserByCredentialsTask', [
            $isClient,
            $email,
            $password,
            $name
        ]);

        Apiato::call('Authorization@AssignUserToRoleTask', [$admin, ['admin']]);

        return $admin;
    }
}

Note: instead of passing these parameters string $email, string $password, string $name, bool $isClient = false from place to another over and over. Consider using the Transporters classes (simple DTO’s “Data Transfer Objects”), for more details read the Transporters Page.

Injecting each Task in the constructor and then using it below through its property is really boring and the more Tasks you use the worse it gets. So instead you can use the function call to call whichever Task you want and then pass any parameters to it.

The Action itself was also called using Apiato::call() which triggers the run function in it.

Refer to the Magical Call page for more info and examples on how to use the call function.

Same Example using the call function:

<?php

namespace App\Containers\User\Actions;

use App\Containers\User\Tasks\DeleteUserTask;
use App\Ship\Parents\Actions\Action;

class DeleteUserAction extends Action
{

    public function run($userId)
    {
        return Apiato::call(DeleteUserTask::class, [$userId]); // <<<<<
    }

}

Example: Calling multiple Tasks:

<?php

namespace App\Containers\Email\Actions;

use App\Containers\Xxx\Tasks\Sample111Task;
use App\Containers\Xxx\Tasks\Sample222Task;
use App\Ship\Parents\Actions\Action;

class DemoAction extends Action
{

    public function run($xxx, $yyy, $zzz)
    {

        $foo = Apiato::call(Sample111Task::class, [$xxx, $yyy]);

        $bar = Apiato::call(Sample222Task::class, [$zzz]);

        // ...

    }

}

Action usage from a Controller:

 <?php
    //...

    public function deleteUser(DeleteUserRequest $request)
    {
        $user = Apiato::call(DeleteUserAction::class, [$request->xxx, $request->yyy]);

        return $this->deleted($user);
    }

    //...

The same Action MAY be called by multiple Controllers (Web, Api, Cli).

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~