Repository navigation
RESTful Pages
One of the API classes for creating pages, FannieRESTfulPage, borrows some concepts from more modern web frameworks.
Many web application frameworks have a built-in concept of routes which are essentially deciding which code will handle a given request. In a real REST system, requests might look like this:
GET /item
GET /item/1
POST /item
GET /item/2
PUT /item/1
DELETE /item/2
PHP does not handle this sort of thing particularly well without lots of [breakable] URL re-writing added to the web server configuration. FannieRESTfulPage defines routes within a single script and does not worry about "pretty" URLs. Routes are determined by looking at the request method and any parameters included with the request. Given an example set of routes:
$__routes = array(
'get',
'get<id>',
'get<id><name>',
'post',
);A regular GET Page.php request would be handled by the first route. A GET Page.php?id=1 request would be handled by the second route. A GET Page.php?id=1&page=2 request would also be handled by the second route. When a request does not match any route exactly, the longest compatible route is chosen. A GET Page.php?id=1&name=core&page=2 would be handled by the third route. Any POST request would be handled by the fourth route.
Each route is handled by one or two class methods. The method names must follow convention so that the routing system can locate the appropriate method(s) for a given route. The first method called is a handler method. Following the example from above, the handler methods should be named get_handler, get_id_handler, get_id_name_handler, and post_handler. The handler method must return true or false. It is similar to FanniePage::preprocess() or BasicPage::preprocess().
The second method is a view method. The view method follows a similar naming convention and the example view methods would be get_view, get_id_view, get_id_name_view and post_view. The view method is called if the handler method returns true or is the handler method is omitted entirely. A route that does not have a handler or view method will result in an error (HTTP 400 - Bad Request).
When routes include parameters, an instance variable of the same name and value is created automatically. If the request is GET Page.php?id=1 then you can expect $this->id = 1.
FannieRESTfulPage defines several common routes by default. Pages do not need to implement them all - in fact, most do not - but the following routes are available "out of the box":
get
get<id>
post
post<id>
put
put<id>
delete
delete<id>
The PUT and DELETE methods can be simulated using the special query parameter _method. A request GET Page.php?id=1&_method=PUT will be treated as a PUT request and match route put<id>. This hack is partially borrowed from Rails. CORE's use of parameters in routes rather than stricly URLs makes using PUT and DELETE correctly difficult.
They cut down a lot of boilerplate code dealing with form submissions. Generally when form data is submitted, the page script should notice that data and do something with it. With RESTful pages you can skip checking whether data has been submitted and manually branching into a different code path to deal with it. They're also significantly more test-friendly. You can easily simulate different user inputs by setting class properties and calling the desired handler or view.