用于构建相当复杂的 PHP Web 服务的设计模式
我刚刚“完成”了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这里对我自己的框架的自我引用,我提前表示歉意 - 因为我不使用其他任何东西,所以我无法帮助你。我不是做广告,因为它不是公开的。
正如我在评论中所说,我认为一个好的 Web 前端框架不应该意味着它是一个差的 Web 服务框架。
因为我对任何流行的 PHP 框架(CodeIgniter、CakePHP、Kohana)处理请求的限制方式及其大小都不满意,所以我编写了一个框架,该框架实际上仅用于两个目的,处理请求和确定要采取的操作,然后将该操作的代码与视图(响应)分开。
我使用的设计模式是这样的:
/users
- 用户列表/user/*
- 由*
所在的值标识的用户。/user/*/delete
- 删除用户/posts
- 列出帖子/post/*
- 查看帖子*
POST
,则与指定函数的路径一起,执行 IEUserActions::saveUser
。它仅在 POST 上执行的原因是为了使输出和输入具有相同的 URL。SecurityManager::authorize
),如果返回false
,它将重定向到您选择的路径。我相信这种设计模式适用于 Web 服务的原因是:
POST
,还检查其他方法)。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:
/users
- User list/user/*
- User identified by the value where*
is./user/*/delete
- Delete the user/posts
- List posts/post/*
- View post*
UserActions::saveUser
to be executed if the HTTP method isPOST
. The reason it's only executed on POST is to enable output and input to have the same URL.SecurityManager::authorize
) to check for authorization and iffalse
is returned, it redirects to a path of your choosing.The reasons I believe this design pattern works well for Web Services:
POST
, check for other methods too).恕我直言,每个基于 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!
我认为您可以使用与简单 Web 应用程序相同的模式。 Restful 服务与 Web 应用程序具有不同的界面,但该界面下的所有内容都是相同的。您可以将 Restful 服务转换为 Web 应用程序,如下所示:
资源是控制器,方法是操作。
例如:
所以你可以使用前端控制器和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:
resource is the controller, METHOD is the action.
For example:
So you can use front controller and MVC.
快速谷歌一下,我看到
我从未使用过其中任何一个
A quick Google and I see
I have never used any of these