B4J Tutorial [BANano] inline php -> server code

Peter had a great idea for a new feature in BANano:
Kiffi said:
Hello Alain,

So you don't get bored ;), I have an idea for another BANano killer feature: ServerCode :)

Imagine, it would be possible to write BANano code that could be used to program on both the client and server sides. Code blocks to be executed on the server side are bracketed with #If #End If (like JavaScript blocks).
...
I liked it! He had already written out the ground rules so we only needed a couple of mails to get on the same page. As a result BANano 1.27 has a PHP connection (we plan support for .asp(x), CGI, python, ... later).

A note beforehand: It is always an Ajax POST command, using JSON to do the communication. This may be extended in the future.

Some new settings one can set in AppStart:
B4X:
BANano.PHP_NAME = "myapi.php"
BANano.PHPHost = "http://localhost"
BANano.PHPAddHeader("Access-Control-Allow-Origin: *")

A very simple example using the new CallInlinePHPWait() method. All we have to pass is the php method name, and a Map with key pairs where: the key is the parameter name and the value the parameter value.
B4X:
public Sub ButtonClicked(event As BANanoEvent)
   ' function name and params are Case Sensitive!
   Dim res As String = BANano.CallInlinePHPWait("SayHello", CreateMap("Name": "BANano"))
   log(res)
End Sub

' a simple php method which says hello!
#if PHP
function SayHello($Name) {
   $ret = Array("answer" => "Hello " .$Name. "!");
   echo json_encode($ret);  
}
#End If

When Building the BANano app, an extra php file will be generated. All one has to do is copy this php file to e.g. a WAMPs /www/ folder et voila: both are talking with each other :).

An example of a generated php file:
B4X:
<?php
  header("Access-Control-Allow-Origin: *");
 
  $rest_json = file_get_contents("php://input");
  $_POST = json_decode($rest_json, true);
 
  $request='';
  if(isset($_POST['request'])){
      $request = $_POST['request'];
      $params = $_POST['params'];
  }

  if (!function_exists($request)) die("invalid request: '" . $request . "'");

  function SayHello($Name) {
     $ret = Array("answer" => "Hello " .$Name. "!");
     echo json_encode($ret);  
  }
 
  $values = array_values($params);
  call_user_func_array($request, $values);
?>

I personally still favour using B4J's jServer to handle all my server related things (it is really powerful and has proven to be quite a bit faster than e.g. php), but if someone needs a BANano solution that just can't affort to use a VPS, there is now an alternative.

BANano 1.27 can be downloaded here: https://www.b4x.com/android/forum/threads/banano-progressive-web-app-library.99740

Alain
 
Last edited:

LWGShane

Well-Known Member
Licensed User
Longtime User
Two questions:
1: Will this eventually lead to a B4PHP?
2: Have you considered opening a Patreon account?
 

alwaysbusy

Expert
Licensed User
Longtime User
Will this eventually lead to a B4PHP
Definitely something I have been thinking about last week. I don't think the transpiling would be a problem, just how to distinguish which B4J code needs to be transpiled to Javascript, and which to PHP.

Have you considered opening a Patreon account?
I don't know that platform. I see it is for artists. Should it also apply to programmers?
 

LWGShane

Well-Known Member
Licensed User
Longtime User

OliverA

Expert
Licensed User
Longtime User

Cableguy

Expert
Licensed User
Longtime User
Patreon is way beyhond being "just for artist"! Almost every youtuber, worthy of such a name, has a patreon account, as main "please keep on doing what you are doing cause you do it good!"...

SO… KEEP ON DOING....
 
Top