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 网站上。

上一篇 下一篇
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~