」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > Pionia 通用服務終極指南。

Pionia 通用服務終極指南。

發佈於2024-08-06
瀏覽:204

The Ultimate Guide to Pionia Generic Services.

Pionia framework is a PHP Rest framework that is changing how we used to develop Rest platforms. Unlike all the existing frameworks, it gives the entire process a whole new look, making the development of APIs much simpler and less boring. This is because it runs on a different and rather 'new' pattern called moonlight.

Per se, moonlight is not a new architecture/pattern, most institutions/companies/developers have been using it but just unnamed. But today, we aren't talking about moonlight, you can read about it in my other article here and even leave your comments.

To bootstrap a new Pionia project, you need to run the following command assuming you already have composer set up.

Let's create a todo_app.

composer create-project pionia/pionia-app todo_app

You can also run the same project using the pionia command like below:

php pionia serve

To watch you logs as they happen in real time, open a second terminal and run the following command:

tail -f server.log

Background on services.

Services in the Pionia framework are the heart, probably the only part you will spend most of your time while developing your APIs. All normal services in Pionia extend Pionia\Request\BaseRestService. A normal service in Pionia may look like this.

namespace application\services;


use Exception;
use Pionia\Request\BaseRestService;
use Pionia\Response\BaseResponse;
use Porm\Porm;

class UserService extends BaseRestService
{
    /**
     * @throws Exception
     */
    protected function login(array $data): BaseResponse
    {
        // password and username are required, without them we won't proceed even
        $this->requires(["username", "password"]);

        $username = $data["username"];
        $password = password_hash($data['password'], PASSWORD_DEFAULT);

        $user = Porm::from('user')->get(['username' => $username, 'password' => $password]);
        //You can do more here, maybe generate a JWT token or add more checks
        // for example if the user is an active or not
        if ($user) {
            return BaseResponse::JsonResponse(0, "Login successful", $user);
        }
        throw new Exception("User with the given username and password not found");
    }

}

After building your service, you need to register it in the switch that shall be handling it from now on. If you are not aware of switches in Pionia, you can read about them here in the docs. So, head over to our switches folder, probably in the MainAppSwitch.php if you have not yet created another one, and register the above service as below in the registerServices method

     /**
     * Register your services here.
     *
     * @return array
     */
    public function registerServices(): array
    {
        return [
            'user' => new UserService(),
            'todo' => new TodoService()
        ];
    }

This is how you get your service to be auto-discovered by the kernel from now on. In typical settings, you would have added a router, and a controller to map to this service, but Pionia approaches things differently. Remember, you can have the same service registered in multiple switches. This is how we achieve the concept of API versioning since every switch is handled by its API endpoint. By default, MainAppSwitch can be accessed on /api/v1/.

In your requests, you can point to this service by sending the following.

// POST http://localhost:8000/api/v1/
{
    "SERVICE": "user",
    "ACTION": "login",
    "username": "pionia",
    "password": "pionia1234"
}

If you notice, ACTION is the name on the action/method we created in our SERVICE/service/class which we baptised the name user upon registration.

This is how normal services work in Pionia.

Below is a complete service that performs CRUD in Piona. It is based on the following simple table called todos in a MySQL database called todo_db.

create table todo_db.todos
(
    id          int auto_increment primary key,
    title       varchar(200)                        not null,
    description text                                null,
    created_at  timestamp default CURRENT_TIMESTAMP null
) engine = InnoDB;
use Exception;
use Pionia\Request\BaseRestService;
use Pionia\Request\PaginationCore;
use Pionia\Response\BaseResponse;
use Porm\exceptions\BaseDatabaseException;
use Porm\Porm;

class TodoService extends BaseRestService
{
    /**
     * Returns all todos
     * @throws Exception
     */
    public function list(): BaseResponse
    {
        $result = Porm::table('todos')
            ->using('db')
            ->columns(['id', 'title', 'description', 'created_at'])
            ->all();

        return BaseResponse::JsonResponse(0, null, $result);
    }

    /**
     * Returns a single todo
     * @throws Exception
     */
    public function details(array $data): BaseResponse
    {
        $this->requires(['id']);
        $id = $data['id'];

        $result = Porm::table('todos')
            ->using('db')
            ->columns(['id', 'title', 'description', 'created_at'])
            ->get(['id' => $id]);

        return BaseResponse::JsonResponse(0, null, $result);
    }

    /**
     * Creates a new todo
     * @throws Exception
     */
    public function create(array $data): BaseResponse
    {
        $this->requires(['title', 'description']);
        $title = $data['title'];
        $description = $data['description'];

        $result = Porm::table('todos')
            ->save(['title' => $title, 'description' => $description]);

        return BaseResponse::JsonResponse(0, 'Todo created successfully', $result);
    }

    /**
     * Updates a todo
     * @throws Exception
     */
    public function update(array $data): BaseResponse
    {
        $this->requires(['id']);

        $id = $data['id'];

        $todo = Porm::table('todos')
            ->get($id); // similar to `get(['id' => $id])`

        // if the todo is not found, we throw an exception
        if (!$todo) {
            throw new BaseDatabaseException('Todo not found');
        }

        $description = $data['description'] ?? $todo->description;
        $title = $data['title'] ?? $todo->title;

        // we update in a transaction as below
        $result= null;
        Porm::table('todos')
            ->inTransaction(function () use ($description, $title, $id, &$result) {
                Porm::table('todos')
                    ->update(['description' => $description, 'title' => $title], $id);

                $result = Porm::table('todos')
                    ->get($id);
            });

        return BaseResponse::JsonResponse(0, "Todo $id updated successfully", $result);
    }

    /**
     * Deletes a todo
     * @throws Exception
     */
    public function delete(array $data): BaseResponse
    {
        $this->requires(['id']);
        $id = $data['id'];

        $todo = Porm::table('todos')
            ->get($id);

        if (!$todo) {
            throw new BaseDatabaseException('Todo not found');
        }

        $deleted = false;
        Porm::table('todos')
            ->inTransaction(function () use ($id, &$deleted) {
                Porm::table('todos')
                    ->delete($id);
                $deleted = true;
            });
        if (!$deleted) {
            throw new BaseDatabaseException('Todo not deleted');
        }
        return BaseResponse::JsonResponse(0, "Todo $id deleted successfully");
    }

    /**
     * Returns a random todo object if the size is not defined or 1,
     * else returns an array of random todos
     * @throws Exception
     */
    public function random($data): BaseResponse
    {
        $size = $data['size'] ?? 1;

        $result = Porm::table('todos')
            ->random($size);

        return BaseResponse::JsonResponse(0, null, $result);
    }

    /**
     * Returns a paginated list of todos
     * @throws Exception
     */
    public function paginatedList(array $data): BaseResponse
    {
        $offset = $data['offset'] ?? 0;
        $limit = $data['limit'] ?? 3;

        $paginator = new PaginationCore($data, 'todos', $limit, $offset, 'db');
        $result = $paginator->paginate();

        return BaseResponse::JsonResponse(0, null, $result);
    }
}

And since our TodoService is already registered, that's all we need to do, no adding extra routes, no adding controllers, just start hitting the actions in your requests and you should get uniform responses from all the above actions.

However much this is not alot to do, and the only thing to do in Pionia(building services), all the above actions in our TodoService can be omitted and we still get the same functionality, This is where our Generic Services come in!

Todo Service, The Generic Way.

If your logic is not more than create, delete, paginate, list, update, delete, or retrieve then Generic Services might be all you need.

Pionia provides both generic services and mixins to use. Mixins can be put together to put up your entire new Generic Service.

Provided mixins include ListMixin, CreateMixin, DeleteMixin, UpdateMixin, RandomMixin and RetrieveMixin. Under the hood, even Generic services are just combining these Mixins while extending GenericService.

Provided Generic Services include RetrieveCreateUpdateService, RetrieveListCreateService, RetrieveListCreateUpdateDeleteService, RetrieveListDeleteService, RetrieveListRandomService, RetrieveListUpdateDeleteService, RetrieveListUpdateService and UniversalGenericService.

If the above Generics don't combine the mixins in the way you want, you can extend GenericService and call all the mixins you want to use thus creating your custom generic service.

Remember, to use mixins, you must be extending Pionia\Generics\Base\GenericService not the normal BaseRestService we extended before. Also, remember that mixins are just PHP traits and should be used that way.

To refactor our TodoService, we shall need the last mentioned generic service, UniversalGenericService as it uses all the defined mixins.

Let's start by changing the class we extend. Refactor this as below

use Pionia\Generics\UniversalGenericService;
// ... rest of the imports

class TodoService extends UniversalGenericService
{
// ... rest of your actions
}

Before we do anything, let's first define the table we want to target in the database. We use the $table property for this. This is a compulsory feature and must be defined for all generic views.

use Pionia\Generics\UniversalGenericService;
// ... rest of the imports

class TodoService extends UniversalGenericService
{
   public string $table = "todo";
// ... rest of your actions
}

Secondly, from our list action, we are defining columns we want to return, however, we are defining all. If you want to return a certain range of columns only, we define the $listColumns(which defaults to all) and pass the columns we want to return. Let's just still pass all though it is the default behavior of the service.

use Pionia\Generics\UniversalGenericService;
// ... rest of the imports

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $listColumns = ['id', 'title', 'description', 'created_at'];

// ... rest of your actions
}

At this point, we can delete the list action from our service. That's complete!

Our second target action is now details. This one can be replaced by defining the $pk_field which defaults to id. Since our primary key field for our todo table is also id, we don't need to define it, we just need to delete it too! Remember, this one also uses the defined $listColumns for columns to return from the DB.
The RetrieveMixin also defines another sister action to this called retrieve, so in your request, you can use ACTION as details or retrieve, the two will perform the same thing.
Since we already have all we need, we can drop the details action too!

Our third action is create. For this, we must define the $createColumns to define those columns we shall be looking for from the request(required) to create a record. Let's add the property now.

use Pionia\Generics\UniversalGenericService;
// ... rest of the imports

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $listColumns = ['id', 'title', 'description', 'created_at'];
    public ?array $createColumns = ['title', 'description'];

// ... rest of your actions
}

After adding, go ahead and delete it too!

Our fourth action is update. For this, we require the $pk_field and can also optionally define the $updateColumns. If undefined, the responsible mixin checks if any of the properties were defined in the request, and will update only those.
Let's add the $updateColumns and give it the only properties we intend to update.

use Pionia\Generics\UniversalGenericService;
// ... rest of the imports

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $listColumns = ['id', 'title', 'description', 'created_at'];
    public ?array $createColumns = ['title', 'description'];
    public ?array $updateColumns = ['title', 'description'];

// ... rest of your actions
}

We can now drop the update action too!

For our fifth action, delete, we only need the $pk_field which is by default id, so we shall be checking if id was passed in the request, and then we delete the associated record. So, just delete it, we already have all we need!

Now to our sixth action, random, this also uses the $listColumns to determine the columns to fetch from the DB per record. We already have out property defined, so, just drop it too!

For our seventh action, paginatedList, we can drop it, and in any request, we target our list action, but we define any of the following pairs of keys in our request.

  1. limit and offset on the request object level.
{
   "SERVICE": "todo",
   "ACTION": "list",
   "limit": 3,
   "offset": 0
}
  1. PAGINATION or pagination object on the request with limit and offset keys.
{
   "SERVICE": "todo",
   "ACTION": "list",
   "PAGINATION": {
      "limit": 3,
      "offset": 0,
   }
}
  1. SEARCH or search object on the request object with limit and offset keys.
{
   "SERVICE": "todo",
   "ACTION": "list",
   "SEARCH": {
      "limit": 3,
      "offset": 0
   }
}

Note: Both the limit and offset keys must be defined for pagination to kick in.

And just like that, our service now has been reduced to the following.

use Pionia\Generics\UniversalGenericService;

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $listColumns = ['id', 'title', 'description', 'created_at'];
    public ?array $createColumns = ['title', 'description'];
    public ?array $updateColumns = ['title', 'description'];
}

Let's do a little more cleanup. As we had mentioned earlier, if we are listing all columns from our table, then we don't need to define the $listColumns property, let's remove that too.

use Pionia\Generics\UniversalGenericService;

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $createColumns = ['title', 'description'];
    public ?array $updateColumns = ['title', 'description'];
}

Also, since our update can also discover the columns to update from the request data, let's remove the $updateColumns too!

And we are left with the following as our new TodoService but still exposing the actions of list(all and paginated), create, update, delete, retrieve or details and random

use Pionia\Generics\UniversalGenericService;

class TodoService extends UniversalGenericService
{
    public string $table = "todo";
    public ?array $createColumns = ['title', 'description'];
}

You can also override how we get a single record and multiple records. You might not need it, but sometimes you may need to add where clauses and other conditions as you see fit. For that, you can read about it in this section in the docs.

Also, you may want to add your other actions in the same generic service, this is fully supported and will work as if you're in normal services, however, make sure none of those actions share the names with the provided mixin actions or otherwise you stand a chance of overriding the provided actions.

This also drives to the last point, what if you intend to override the default action? that's also okay! You can also look into it under this section of the docs.

Welcome to Pionia Framework, where we believe in both developer and program performance, writing precise and maintainable codebase with simplicity both at the front end and the back end!

Let me hear what you say about the Pionia Framework specifically about generic services. Happy coding!

版本聲明 本文轉載於:https://dev.to/jet_ezra/the-ultimate-guide-to-pionia-generic-services-id?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 優化 AWS ECS 的 Java 堆設置
    優化 AWS ECS 的 Java 堆設置
    我們在 AWS Elastic Container Service(ECS) Fargate 上執行多個 Java 服務 (Corretto JDK21)。每個服務都有自己的容器,我們希望使用為每個進程支付的所有可能的資源。但這些步驟可以應用於 EC2 和其他雲端。 服務正在運行批次作業,延遲並不...
    程式設計 發佈於2024-11-06
  • PHP 初學者必備知識:釋放網站的全部潛力
    PHP 初學者必備知識:釋放網站的全部潛力
    PHP基礎:釋放網站潛能PHP是強大的伺服器端腳本語言,廣泛用於建立動態網站。對於初學者來說,掌握PHP基礎知識至關重要。本文將提供一個全面的指南,涵蓋PHP編程的基本要素,並透過實戰案例鞏固理解。 安裝並設定PHP要開始使用PHP,您需要安裝PHP解釋器和相關的軟體。遵循以下步驟:- 下载并安装P...
    程式設計 發佈於2024-11-06
  • 如何確定 PHP 標頭的正確圖片內容類型?
    如何確定 PHP 標頭的正確圖片內容類型?
    確定PHP 標頭的圖像內容類型確定PHP 標頭的圖像內容類型使用Header() 函數從Web 根目錄之外顯示圖像時,用戶可能會遇到困惑關於指定的內容類型:image/png。然而,儘管內容類型固定,但具有各種擴展名的圖像(例如, JPG、GIF)仍然可以成功顯示。 $filename = base...
    程式設計 發佈於2024-11-05
  • ByteBuddies:使用 Python 和 Tkinter 建立互動式動畫寵物
    ByteBuddies:使用 Python 和 Tkinter 建立互動式動畫寵物
    大家好! 我很高興向大家介紹 ByteBuddies,這是一個用 Python 和 Tkinter 創建的個人項目,展示了互動式動畫虛擬寵物。 ByteBuddies 將引人入勝的動畫與使用者交互相結合,提供了展示 GUI 程式設計強大功能的獨特體驗。該項目旨在透過提供互動式虛擬寵物來讓您的螢幕充...
    程式設計 發佈於2024-11-05
  • 如何解決“TypeError:\'str\'物件不支援專案分配”錯誤?
    如何解決“TypeError:\'str\'物件不支援專案分配”錯誤?
    'str'物件項目分配錯誤疑難排解'str'物件項目分配錯誤疑難排解嘗試在Python 中修改字串中的特定字元時,您可能會遇到錯誤「類型錯誤:「str」物件不支援專案分配。」發生這種情況是因為Python 中的字串是不可變的,這意味著它們無法就地更改。 >>...
    程式設計 發佈於2024-11-05
  • 如何緩解 GenAI 程式碼和 LLM 整合中的安全問題
    如何緩解 GenAI 程式碼和 LLM 整合中的安全問題
    GitHub Copilot and other AI coding tools have transformed how we write code and promise a leap in developer productivity. But they also introduce new ...
    程式設計 發佈於2024-11-05
  • Spring 中的 ContextLoaderListener:必要的邪惡還是不必要的複雜?
    Spring 中的 ContextLoaderListener:必要的邪惡還是不必要的複雜?
    ContextLoaderListener:必要的邪惡還是不必要的複雜? 開發人員經常遇到在 Spring Web 應用程式中使用 ContextLoaderListener 和 DispatcherServlet。然而,一個令人煩惱的問題出現了:為什麼不簡單地使用 DispatcherServle...
    程式設計 發佈於2024-11-05
  • JavaScript 機器學習入門:TensorFlow.js 初學者指南
    JavaScript 機器學習入門:TensorFlow.js 初學者指南
    機器學習 (ML) 迅速改變了軟體開發世界。直到最近,由於 TensorFlow 和 PyTorch 等函式庫,Python 仍是 ML 領域的主導語言。但隨著 TensorFlow.js 的興起,JavaScript 開發人員現在可以深入令人興奮的機器學習世界,使用熟悉的語法直接在瀏覽器或 Nod...
    程式設計 發佈於2024-11-05
  • extjs API 查詢參數範例
    extjs API 查詢參數範例
    API 查詢 參數是附加到 API 請求 URL 的鍵值對,用於傳送附加資訊至伺服器。它們允許用戶端(例如 Web 瀏覽器或應用程式)在向伺服器發出請求時指定某些條件或傳遞資料。 查詢參數加入到 URL 末端問號 (?) 後。每個參數都是鍵值對,鍵和值之間以等號 (=) 分隔。如果有多個查詢參數,...
    程式設計 發佈於2024-11-05
  • 如何解決Go中從不同套件匯入Proto檔案時出現「Missing Method Protoreflect」錯誤?
    如何解決Go中從不同套件匯入Proto檔案時出現「Missing Method Protoreflect」錯誤?
    如何從不同的套件導入Proto 檔案而不遇到「Missing Method Protoreflect」錯誤在Go 中,protobuf 常用於資料序列化。將 protobuf 組織到不同的套件中時,可能會遇到與缺少 ProtoReflect 方法相關的錯誤。當嘗試將資料解組到單獨套件中定義的自訂 p...
    程式設計 發佈於2024-11-05
  • 為什麼MySQL在查詢「Field = 0」非數位資料時傳回所有行?
    為什麼MySQL在查詢「Field = 0」非數位資料時傳回所有行?
    不明確的查詢:理解為什麼MySQL 回傳「Field=0」的所有行在MySQL 查詢領域,一個看似無害的比較,例如“SELECT * FROM table WHERE email=0”,可能會產生意外的結果。它沒有按預期過濾特定行,而是返回表中的所有記錄,從而引發了對資料安全性和查詢完整性的擔憂。 ...
    程式設計 發佈於2024-11-05
  • 伺服器發送事件 (SSE) 的工作原理
    伺服器發送事件 (SSE) 的工作原理
    SSE(服务器发送事件)在 Web 开发领域并未广泛使用,本文将深入探讨 SSE 是什么、它是如何工作的以及它如何受益您的申请。 什么是上交所? SSE 是一种通过 HTTP 连接从服务器向客户端发送实时更新的简单而有效的方法。它是 HTML5 规范的一部分,并受到所有现代 Web ...
    程式設計 發佈於2024-11-05
  • 如何從字串 TraceID 建立 OpenTelemetry Span?
    如何從字串 TraceID 建立 OpenTelemetry Span?
    從字串 TraceID 建構 OpenTelemetry Span要建立 Span 之間的父子關係,必須在上下文傳播不可行的情況下使用標頭。在這種情況下,追蹤 ID 和跨度 ID 包含在訊息代理程式的標頭中,這允許訂閱者使用父追蹤 ID 建立新的跨度。 解決方案以下步驟可以使用追蹤ID 在訂閱者端建...
    程式設計 發佈於2024-11-05
  • 如何在gRPC中實現伺服器到客戶端的廣播?
    如何在gRPC中實現伺服器到客戶端的廣播?
    gRPC 中的廣播:伺服器到客戶端通訊建立gRPC 連線時,通常需要將事件或更新從伺服器廣播到客戶端連接的客戶端。為了實現這一點,可以採用各種方法。 Stream Observables常見的方法是利用伺服器端流。每個連線的客戶端都與伺服器建立自己的流。然而,直接訂閱其他伺服器客戶端流是不可行的。 ...
    程式設計 發佈於2024-11-05
  • 為什麼填入在 Safari 和 IE 選擇清單中不起作用?
    為什麼填入在 Safari 和 IE 選擇清單中不起作用?
    在Safari 和IE 的選擇清單中不顯示填充儘管W3 規範中沒有限制,但WebKit 瀏覽器不支援選擇框中的填充,包括Safari和Chrome。因此,這些瀏覽器中不應用填充。 要解決此問題,請考慮使用 text-indent 而不是 padding-left。透過相應增加選擇框的寬度來保持相同的...
    程式設計 發佈於2024-11-05

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3