- Getting started
- Basics
- Finding
- Relation Mapping
- Models
- Database
- BeanCan
- BeanCan Server
- Advanced
- Architecture
- Other
BeanCan Server
BeanCan is a PHP class that can act as a backend server for Javascript centered web applications (JSON-RPC 2.0 compliant). In a JS based web application your views and controllers are written in client-side Javascript while your models are still stored on the server. BeanCan acts as bridge between the client side javascript views and controllers on the one hand and the server side models on the other.
BeanCan makes use of FUSE. This means that you can send 4 types of commands to the BeanCan Server:
| Command: |
|---|
| load |
| store |
| trash |
| custom |
Requests 1-4 are handled automatically by RedBeanPHP. This means you can store/delete/load any bean automatically if you connect to the bean server without *any* effort. If you send an unrecognized command, FUSE tries to locate the model and passes the request. Time for examples...
Request #1: The following request returns a page with ID 1:
{
"jsonrpc":"2.0",
"method":"page:load",
"params":[1],
"id":"myrequestid"
}
Request #2: The following request creates a new page and returns its new ID:
{
"jsonrpc":"2.0",
"method":"page:store",
"params":[{"body":"lorem ipsum"}],
"id":"myrequestid"
}
Request #3: The following request changes the text of page 2:
{
"jsonrpc":"2.0",
"method":"page:store",
"params":[{"body":"welcome","id":2}],
"id":"myrequestid"
}
Request #4: This example request deletes page with ID 3:
{
"jsonrpc":"2.0",
"method":"page:trash",
"params":[3],
"id":"myrequestid"
}
Request #5: executes $page->mayAccess( $ip ) and returns the result. FUSE will connect automatically to the Model_Page class to accomplish this.
{
"jsonrpc":"2.0",
"method":"page:mayAccess",
"params":[ ipAddress ],
"id":"myrequestid"
}
The BeanCan server returns JSON reponses like this (created page and returns ID):
{
"jsonrpc":"2.0",
"result":"8",
"id":"myrequestid"
}
In case of an error:
{
"jsonrpc":"2.0",
"error":{"code":"-32603","message":"Invalid request"},
"id":"myrequestid"
}
Full Example
Here is a full example. It is a todo list written in Javascript and PHP using the BeanCan Server.
Not familiar with JSON-RPC ? Take a look at: JSON-RPC specification.
REST server
In RedBeanPHP 3.0 the BeanCan server also responds to RESTFul GET requests. To setup a REST server with beancan:
$server = new RedBean_BeanCan();
$server->handleRESTGetRequest('/book/2'); //returns book with ID 2
$server->handleRESTGetRequest('/book'); //returns books
Tweet
User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.
Site comments powered by Disqus