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.
PHP Design patterns is an Object-Oriented Programming (OOP) concept that is now also used in Drupal 9 projects. With Drupal's adoption of modern PHP and OOP concepts since version 8, design patterns can be leveraged for cleaner and more robust programming.
Many GoF design patterns can be applied to Web service application design.
4 Design Patterns You Should Know for Web Development: Observer, Singleton, Strategy, and Decorator.
RESTful APIs should take advantage of HTTP methods, or verbs, such as GET, PUT, and POST. RESTful API Design Patterns: API design patterns provide a description or templates to solve specific, recurring API design problems that any software architects and API designers would like to adopt in their API designs.
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 is POST
. 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 if false
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).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With