gpt4 book ai didi

php - 为我的 php 应用程序(社交应用程序)创建 API 版本控制

转载 作者:可可西里 更新时间:2023-11-01 12:53:55 26 4
gpt4 key购买 nike

我目前正在开发社交应用程序。

我想在我的 PHP 后端有一个版本控制系统,但我不知道从哪里开始:我在网上查了一堆文章,但我不太明白我找到了什么。

假设我的应用程序是 v1.0。然后我创建了一个新版本 v2.0,当然我也更新了 PHP 文件。在此之后,如果有人还没有从 v1.0 更新他们的应用程序,我希望他们访问 myapp.com/v1.0/ 这样应用程序就不会崩溃。

你会推荐什么?

我的一个典型的 PHP 文件如下所示,例如:

<?php

// Include files

include_once ('../../config.php');

include_once (ROOT_DIR . '/config/includes.php');

$_USERDATA = Validator::validateUser($_POST["userId"], $_POST["session"]);

// Valid session

$qry = $db->prepare('SELECT n.id, n.type, n.time, n.isRead, n.postId, n.commentId, n.text, n.inReplyToCommentId, u.id as byUserId,
( SELECT COUNT(nu.id)
FROM notificationUsers AS nu
WHERE nu.notificationId = n.id ) as detail1,
( SELECT nu2.userId
FROM notificationUsers AS nu2
WHERE nu2.notificationId = n.id ORDER BY nu2.id DESC LIMIT 1 ) as latestUserId FROM notifications AS n LEFT JOIN userData AS u ON n.byUserId = u.id
WHERE n.userId = :userId ORDER BY n.time DESC');
$qry->bindParam(':userId', $_USERDATA["userId"], PDO::PARAM_INT);
$qry->execute();

$notificationsArray = $qry->fetchAll(PDO::FETCH_ASSOC);


$returnValue = array();
$returnValue["status"] = "1";
$returnValue["title"] = "Success";
$returnValue["message"] = "Downloaded notifications";
$returnValue["data_notifications"] = $notificationsArray;
exit(json_encode($returnValue));



?>

...

更新 1

所以我决定做以下事情:

像这样将每个不共享的文件放在一个文件夹中:

/api/v1.0/file.php

但是每个资源(例如图像)都在 API 之外

/resources/images/profilepictures/image.jpg

如果我做了一个小改动,我只更新 api/v1.0/ 文件夹中的文件。

然后,所有使用 Application v1.1 的用户,他们正在请求 myapp.com/api/v1.1/,但我将他们重定向到api/v1.0/我可以在其中向用户展示正确的数据(取决于请求是来自 v1.0 还是 v1.1)

如果版本加起来并且我发布了更大的更新,我将创建一个名为 v2.0 的新文件夹,然后就这样......

或者你怎么看?我应该怎么做?

最佳答案

您定义的架构是为了让您的设计面向 future ,这是良好的开端。

如果您使用像 Laravel 或 Symfony 这样的框架,您可以让您的 Models 和 Helpers 目录保持通用,同时使用 v1.x、v2.x 等子目录对 Controllers 目录进行版本控制。

如果您使用路由机制,它可以让您的生活变得轻松,例如,您的 v1.0 有 50 个 API 端点,您计划修改其中的 5 个以实现 v1.2 w/o 向后兼容性,在在这种情况下,其余 45 个 API 端点仍将与 v1.0 相同,您不想在 v1.2 目录中复制它们。

<?php

$versions = ["v1.0", "v1.2"];

route("/api/v1.0/foobar", "Controllers/v1.0/FoobarController@doSomething");
route("/api/v1.0/footar", "Controllers/v1.0/FoobarController@doTar");

route("/api/v1.2/foobar", "Controllers/v1.2/FoobarController@doSomething");

function route($endpoint, $controller)
{
// Logic to call function of a respective class/file
// and call previous version of an API if function/endpoint is not redefined in current version
}
?>

所以在上面的例子中,当客户端调用/api/v1.2/footar时,它应该为API消费者执行/api/v1.0/footar。

希望对你有帮助。

关于php - 为我的 php 应用程序(社交应用程序)创建 API 版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56350079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com