1. Go to this page and download the library: Download lucaj/php-rhino library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lucaj / php-rhino example snippets
$app = rhino();
$app->use($jsonparse);
$app->post('/', function($req, $res) {
$name = $req->body['name'];
$res->send("Hello $name");
});
$app->get('/', function($req, $res) {
$res->send("Hello, World!");
});
$app->get('/:name', function($req, $res) {
$res->send("Hello " . $req->params['name']);
});
$app->start();
#+END_SRC
To immediately test your application open a terminal in the root folder of your
application and launch a php development server by entering:
#+BEGIN_SRC
php -S localhost:5001 .
#+END_SRC
Open your favorite browser and navigate to =localhost:5001/Peter= to view the
results. If you want to test the registered *POST* route as well you can use an
external graphical tool like /Postman/ or simply use cURL from the terminal
with the following command:
#+BEGIN_SRC
echo '{ "name": "Charlie" }' | curl -d @- http://localhost:5001/ --header "Content-Type:application/json"
#+END_SRC
Take a look in the =examples/= folder for more advanced working prototype
applications.
** Feature Tutorials
*** Regular Expressions In Resource Routes
Most regular expressions work with route handlers.
#+BEGIN_SRC php
// trigger this route handler as a middleware for all routes starting
// with `/api/`.
$app->use('/api/*', function($req, $res) {
});
// trigger this route handler for any number entered after `/api/`
$app->get('/api/[0-9]+', function($req, $res) {
});
$app->get('/api/*/name/[A-Za-z ]+', function($req, $res) {
});
#+END_SRC
*** Query Parameters
Query Parameters are automatically converted to key-value pairs and
stored in the =query= property of the request object.
#+BEGIN_SRC php
$app->get('/', function($req, $res) {
$orderBy = $req->query['orderBy'];
$offset = $req->query['offset'];
$limit = $req->query['limit'];
});
#+END_SRC
*** Route Parameters
Route parameters are defined with a colon =:= in the resource route.
Route parameters and strings entered by the client in place of the
route parameters are converted to key - value pairs and stored in the
=params= property of the request object.
#+BEGIN_SRC php
$app->get('/api/users/:id', function($req, $res) {
$res->send("Retrieving data for user with id: {$req->params['id']}");
});
$app->get('/api/users/:lastname/:firstname', function($req, $res) {
$lastName = $req->params['lastname'];
$firstName = $req->params['firstname'];
$res->send("Loading data for $firstName $lastName");
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.