This is an outdated version of the Manual. Visit the NEW Manual

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
export (since 3.3)
store
trash
custom

Requests 1-5 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...

From version 3.3 on you can use 'export'. Export works the same as 'load' but returns an entire bean hierarchy instead of just one bean.

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.

Whitelist

To prevent API users from accessing all beans you can use a white list:


    $server
->setWhitelist(array(
        
'candy' => array('store''like')
    ));

This example will only allow you to store candy beans and invoke the custom method 'like'. Other beans and other methods will not be accessible. To turn off the white list and allow full access:


    $sever
->setWhitelist('all');

Not familiar with JSON-RPC ? Take a look at: JSON-RPC specification.


 
 

RedBeanPHP Easy ORM for PHP © 2024 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2