首页 > 解决方案 > 我们可以在管理中使用 API 来管理这些数据吗?

问题描述

您好,我正在做这个主题,以了解有关我正在开发的 Web 应用程序的更多详细信息。

我目前在前端使用 react,对于后端,我使用我开发的 php poo api。

我想知道我是否仍然可以在管理中使用前台并继续使用我的 API 来管理我的数据,无论是读写还是更新?

使用 API 来管理我的数据是一种好习惯吗?

示例 Api 后端:

<?php

// Headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: GET");
header("Content-Type: application/json; charset=UTF-8");

include('../../config/Database.php');
include('../../models/post.php');

// DB CONNECT

if($_SERVER['REQUEST_METHOD'] == 'GET') {

    $database = new Database();
    $db = $database->connect();

    $post = new Post($db);
    $stmt = $post->read();

    if($stmt->rowCount() > 0) {
        $arrayPost = [];
        $arrayPost['post'] = [];

        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            $posts= [
                'id' => $id,
                'title' => $title,
                'user_id' => $user_id,
                'categorie_id' => $categorie_id,
                'body' => utf8_decode($body),
                'author' => $author,
                'resolved' => $resolved
            ];

            $arrayPost['post'][] = $posts;
        }

        http_response_code(200);
        echo json_encode($arrayPost);
    }

}else {
    http_response_code(405);
    echo json_encode(["message" => "La méthode n'est pas autorisée"]);
}

我的 API 的示例恢复:

await axios.get(urlPost).then(res => {
    console.log(data)
  })
  .catch(err => {
    console.log(err.message)
  })

如果我在前面创建管理,我可以继续使用我的 API 来编辑帖子或用户甚至删除吗?我想知道这是否是一个好习惯

标签: phpapifrontendback

解决方案


推荐阅读