用于构建相当复杂的 PHP Web 服务的设计模式

发布于 2024-10-14 21:00:12 字数 265 浏览 1 评论 0原文

我刚刚“完成”了 PHP 中一个相对复杂的 Web 服务的编码。由于最后一刻的请求、更改、附加组件等常见问题,代码库现在有点混乱。

我尝试尽可能简单地编写代码,并以最大限度提高性能的方式编写代码。

因此,我没有使用任何像 Zend 这样的框架或任何像 Doctrine 这样的 ORM。

我想知道是否存在专门用于在 PHP 中构建 API/Web 服务的框架或设计模式?

我正在考虑重构,我想确保现在我确切地知道所涉及的内容,我可以正确地构建这个东西。

I have just "finished" coding up a relatively involved web service in PHP. The code base is now a bit of a mess due to last minute requests, changes, add-ons, the usual.

I tried to code it as lightly as possible and in a manner that would maximise performance.

Therefore, I didn't use any frameworks like Zend or any ORMs like Doctrine.

I was wondering if there are any frameworks or design patterns that exist for the sole purpose of building APIs/web services in PHP?

I'm thinking of a refactor and I want to make sure now I know exactly what's involved I can build this thing properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-10-21 21:00:12

对于这里对我自己的框架的自我引用,我提前表示歉意 - 因为我不使用其他任何东西,所以我无法帮助你。我不是做广告,因为它不是公开的。

正如我在评论中所说,我认为一个好的 Web 前端框架不应该意味着它是一个差的 Web 服务框架。

因为我对任何流行的 PHP 框架(CodeIgniter、CakePHP、Kohana)处理请求的限制方式及其大小都不满意,所以我编写了一个框架,该框架实际上仅用于两个目的,处理请求和确定要采取的操作,然后将该操作的代码与视图(响应)分开

我使用的设计模式是这样的:

  1. 所有 URL 都被重写(mod_rewrite)并传递到您的执行入口点。
  2. 您的入口点设置它将识别和处理的路径。用于 Web 服务的 IE:
    • /users - 用户列表
    • /user/* - 由 * 所在的值标识的用户。
    • /user/*/delete - 删除用户
    • /posts - 列出帖子
    • /post/* - 查看帖子*
  3. 如果 HTTP 方法是 POST,则与指定函数的路径一起,执行 IE UserActions::saveUser。它仅在 POST 上执行的原因是为了使输出和输入具有相同的 URL。
  4. 该路径还指定一个视图。这是将发送到浏览器的响应正文。它可以直接呈现为 PHP,也可以插入模板引擎。对于 Web 服务,所有路径可能都会使用单个视图,以输出格式(JSON、XML 等)呈现数据。该视图可以只是一个 PHP 方法,不需要指定模板文件。
  5. 对于 Web 前端,视图可以有一个包装它的父视图(从内到外创建页面)。
  6. 最后一点是安全性。您可以定义要应用于任何路径的安全类型。安全类型仅指定检查授权的函数(例如 SecurityManager::authorize),如果返回 false,它将重定向到您选择的路径。

我相信这种设计模式适用于 Web 服务的原因是:

  • 使您能够使用单入口点,但可以与多个入口点一起使用(用于优化,如果需要)。
  • 不要假设您希望 URL 与对象模型相匹配,就像大多数主要框架所做的那样(Zend 是一个值得注意的例外,如评论中所述)。
  • 轻松适应 REST(不仅检查 POST,还检查其他方法)。
  • 删除任何 HTML 感觉是完全自然的,因为在这种模式中,响应与处理完全分开。
  • 这一切都可以在几堂课中完成。

I apologize in advance for the self-reference here to my own framework - there's no way for me to help you otherwise since I don't use anything else. I'm not advertising, since it's not public.

As I said in my comment, I think a good web front-end framework shouldn't mean it is a poor web service framework.

Because I was unsatisfied with the restrictive way any of the popular PHP frameworks (CodeIgniter, CakePHP, Kohana) processed requests, as well as their size, I wrote a framework that is designed for really only two purposes, process a request and determine an action to take, and then separate the code for that action from the view (response).

The design pattern I use is this:

  1. All URLs are rewritten (mod_rewrite) and passed to your execution entry point.
  2. Your entry point sets up paths that it will recognize and process. I.E. for a web service:
    • /users - User list
    • /user/* - User identified by the value where * is.
    • /user/*/delete - Delete the user
    • /posts - List posts
    • /post/* - View post *
  3. Along with the path you specify a function, I.E. UserActions::saveUser to be executed if the HTTP method is POST. The reason it's only executed on POST is to enable output and input to have the same URL.
  4. The path also specifies a view. This is the response body that will be sent to the browser. It can be rendered as straight PHP, or you could plug in a template engine. In the case of web services, all paths would probably use a single view that renders your data in the output format (JSON, XML, whatever). The view can be just a PHP method and is not required to specify a template file.
  5. In the case of a web front-end, the view can have a parent view which wraps it (creating the page from the inside-out).
  6. The last point is security. You can define a security type to be applied to any path. A security type just specifies what function (like SecurityManager::authorize) to check for authorization and if false is returned, it redirects to a path of your choosing.

The reasons I believe this design pattern works well for Web Services:

  • Enables you to use a single-entry point, but can be used with multiple entry points (for optimization, if needed).
  • No assuming that you want your URLs to match your Object Model, like most of the major frameworks do (a notable exception being Zend, as mentioned in the comments).
  • Easily adapted to REST (instead of just checking for POST, check for other methods too).
  • The removal of any HTML feels completely natural, since in this pattern the response is completely separated from processing.
  • This can all be done in a few classes.
感受沵的脚步 2024-10-21 21:00:12

恕我直言,每个基于 MVC 的“东西”都可以真正帮助您。

如果你真的不想使用任何已经存在的东西(尝试一下 CakePHP!),那么按照 mvc 构建你的代码确实可以帮助您将应用程序的逻辑拆分到更多层,并使其更具可读性和可调试性。

当然,使用更好的模式,您也可以编写糟糕的代码,这取决于您!

Imho, every MVC-based "thing" can really help you.

If you really do not want to use anything (give a try to CakePHP!) already existing, strucutring your code following mvc can really help you to split the logic of your application on more layer, and keep it more readable and debuggable.

Of course, also with the better pattern you can write awful code, it's up to you!

血之狂魔 2024-10-21 21:00:12

我认为您可以使用与简单 Web 应用程序相同的模式。 Restful 服务与 Web 应用程序具有不同的界面,但该界面下的所有内容都是相同的。您可以将 Restful 服务转换为 Web 应用程序,如下所示:

METHOD host/resource/data => host/resource/METHOD?data

资源是控制器,方法是操作。
例如:

GET http://library.com/books/123 => http://library.com/books/get?123

所以你可以使用前端控制器和MVC。

I think you can use the same patterns you use by simple web applications. A restful service has different interface than a web application, but everything under that interface is the same. You can transform a restful service to a web application like so:

METHOD host/resource/data => host/resource/METHOD?data

resource is the controller, METHOD is the action.
For example:

GET http://library.com/books/123 => http://library.com/books/get?123

So you can use front controller and MVC.

难理解 2024-10-21 21:00:12

快速谷歌一下,我看到

我从未使用过其中任何一个

A quick Google and I see

I have never used any of these

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文