JAX-RS-like PHP framework for RESTful Web Services

##PHPRS github

PHPRS is a lightweight, easy-to-use, jax-rs-like and open source library for RESTful Web Services.

Examples

“orders manage”

/**
 * @path("/orders/")
 */
class Orders
{
    /** 
     * @route({"GET","/"})
     * @return({"body"})
     */
    public function getAllOrders() {
        return Sql::select('*')->from('orders')->get($this->pdo);
    }
    /** 
     * @route({"GET","/*"})
     * @param({"id", "$.path[1]"})
      * @return({"body"})
     */
    public function getOrderById($id) {
        return Sql::select('*')->from('orders')->where('id=?',$id)->get($this->pdo);
    }
    
    /** 
     * @route({"POST","/*"})
     * @param({"goods_info", "$._POST.goods"})
     * @return({"body"})
     */
    public function createOrder($goods_info){
        $order_id = Sql::insertInto('orders')->values($goods_info)->exec($this->pdo)->lastInsertId();
        return ['order_id'=>$order_id];
    }
    /**
     * Instance of class \PDO
     * @property 
     */
    public $pdo;
}

Features

  1. Flexible routes

    PHPRS use @route to define routes.

    @route({"GET","/patha"})                  |   GET     | /patha, /patha/...
    ------------------------------------------+-----------+---------------------
    @route({"*","/patha"})                    |   GET     | /patha
                                              |   POST    |
                                              |   PUT     |
                                              |   DELETE  |
                                              |   HEAD    |
                                              |   ...     |
    ------------------------------------------+-----------+---------------------
    @route({"GET","\patha\*\pathb"})          |   GET     | /patha/xxx/pathb
    ------------------------------------------+-----------+---------------------
    @route({"GET", "func1?param1=1&param2=2"})|   GET     | /func1?param1=1&param2=2&...
                                              |           | /myapi/func1?param2=2&param1=1&...
    
  2. Two-way parameter binding

    Annotations: @param, @return,@throws is used to bind variables between function parameters and http request or response.

    ------------------------------------------+-----------------------------
    @param({"arg0","$._GET.arg0"})            | $arg0 = $_GET['arg0']
    ------------------------------------------+-----------------------------
    @param({"arg1","$.path[1]"})              | $arg1 = explode('/', REQUEST_URI)[1]
    ------------------------------------------+-----------------------------
    @return({"cookie","token","$arg2"})       | setcookie('token', $arg2)
    function testCookie(&$arg2)               |
    ------------------------------------------+-----------------------------
    @return({"body"})                         | use function return as http response body
    ------------------------------------------+-----------------------------
    @throws({"MyException",                   | try{}
        "res",                                | catch(MyException) {
        "400 Bad Request",                    |   header("HTTP/1.1 400 Bad Request");body(["error"=>"my exception"]);}
        {"error":"my exception"}}) 
    
  3. Api cache

    Use @cache to enable cache for this method. If all params of the method are identical, the following calls will use cache.

     ----------------------------------+-----------------------------
     @cache({"ttl",3600})              | set cache as fixed time expire, as ttl 1 hour.
     ------------------------------------------+-----------------------------
     @cache({"checker", "$checker"})   | Use dynamic strategy to check caches. 
                                       | $check is set in method, and will be invoked to check cache expired with $check($data, $create_time), for examples use $check = new FileExpiredChecker('file.tmp'); to make cache invalidated if file.tmp modified.
    
  4. Dependency Injection
    Use @property to inject dependency
    phprs create API class and inject dependency from conf.json, which is looks like

    {
       "Orders":{
            "properties": {
                "db":"@db"
            }
       },
       "db":{
            "singleton":true,
            "class":"PDO",
            "pass_by_construct":true,
            "properties":{
                "dsn":"mysql:host=127.0.0.1;dbname=testdb;",
                "username":"test",
                "passwd":"test"  		
            }
       }
    }
    
  5. Document automatic generation
    The document is looked like:

  6. Hook
    The implement of a hook is the same as API.

Hey @Cayman,

So that is sort of interesting, just curious if you have a roadmap for future features or a todo list of some sort (in case someone wonders by this posts and is looking for a project to get involved with).

Also, what would you say differentiates your library from others? What makes yours easier to use, safer, and so forth?

No built in authentication guard for limiting access to service calls? Isn’t that a basic feature of something like this. I don’t really understand the benefit of using this over a well known framework where building an api is typically easy enough. Not to mention I don’t know why you’re using a proprietary database abstraction layer. That just seems like amateur hour.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.