Location: PHPKode > scripts > REST Server > rest-server/DOC.txt

The aim of this class is to Map URLs to Controller classes. The main class is the RestServer, wich is responsable for holding the Request, the Response, the Url map and make decision about what to load after. This is intended for RESTful webservices or websites. 

You can map an URL by calling the method addMap() on a RestServer instance, as above:

$rest = new RestServer ;

You can also pass the url to treat for the RestServer, instead of the requested Url with:
$rest = new RestServer($_GET["url"]);

The addMap() method takes three arguments, as follow:
1- The Request Method, ie:
1.1- GET, POST, PUT, DELETE, OPTION and others.
2- The URL pattern, it's a regular expression pearl compatible(but without delimiters), that means the following examples:
2.1- "/","/user","/device/[a-z0-9]+","/post/[0-9]+/comments","/article/[0-9]*/photo/[0-9]*".
3- A controller/view class, that must implement RestController or RestView, no name conventions.
3.1- Can alse specify class method like "Profile::full",but it will not be called staticly.

The Controller(prefered) or View to receive the request must implement RestController or RestView. The RestController should implement the execute() method receiving a RestServer as a parameter, as follow:

class Anything implements RestController {
    function execute(RestServer $rest) {
        // Logic here
        return $rest ;

And the RestView implementation must use show() method also receiving a RestServer, as follow:

class Anyview implements RestView {
    function show(RestServer $rest) {
        //Logic here
        return $rest;

Both need to have a parameter-free public constructor(or no contructor at all). Alternative methods must also receive the RestServer as a parameter. Ideally it should always return something, but that's not mandatory. One can return the RestServer, ending the session, or a RestAction(wich is any RestController or RestView) to be taken next. Example:

class Anything implements RestController {
    function execute(RestServer $rest) {
        return new Anyview ;

class Anyview implements RestView {
    function show(RestServer $rest) {
        return $rest;

For recovering information about the request taken, the RestServer provides the RestRequest object. You can get it by calling the getRequest() method on the RestServer. There are a few method available on the RestRequest, following:

isGet() // If is a GET
isPost() // If is a POST
isPut() // PUT
isDelete() // Delete
getGet($k=null) // return the $_GET
getPost($k=null) // $_POST
getFiles($k=null) // $_FILES
getPut($k=null) // Put uploaded content
getAuthData() // Authentication data for DIGEST auth
getUser() // User for Basic authentication
getPassword() // Password for Basic authentication
getMethod() // the Request Method(get, put, post)
setMethod($m) // set the Request Method, for action forwarding
getRequestURI() // get the URI
getURIpart($i) // get Part of the URL, divided by "/"
getExtension() // get the URI extension(html, xml, json)
acceptMime($mime) // if Request accept given MIME type

To build up a response, the RestServer provide the Response Object, access it by calling the method getResponse() on the RestServer. The RestResponse gives you the following methods:

cleanResponse() // Erease any response
addHeader($header) // Adds a HTTP Header
cleanHeader() // Erease any header
showHeader() // Show headers
headerSent() // If headers were sent
setResponse($response) // Set the response content, could be a string(like a HTML document) or a RestAction
addResponse($response) // Append a response, string only
getResponse() // get the Response content(or class, if RestAction)

Also the RestServer provides a few methods it self:

setParameter($key,$value) // Set a parameter to be kept all over
getParameter($key) // Retrieves a parameter
setQuery($value,$k=null) // Set the query(uri) to deal, instead of the Resquest RequestURI
addMap($method,$uri,$class) // Already covered
isAuth() // If is authenticated
setAuth($bool) // Set if is authenticated
getQuery($k=null) // get part of the Query(URI)
getBaseUrl() // Base URL of the service
getResponse() // the RestResponse object
getRequest() // the RestRequest object

See example for more information.

This is develop by Diogo Souza da Silva <hide@address.com>.
Return current item: REST Server