PHP code example of nexus-rest-api / nexus

1. Go to this page and download the library: Download nexus-rest-api/nexus 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/ */

    

nexus-rest-api / nexus example snippets



  use Nexus\App;

  $config = [
    'prefix' => 'api',
    'db_connection' => [
      'host' => 'localhost',
      'user' => 'root',
      'password' => '',
      'database' => 'nexus'
      'port' => 3306 // optional (default 3306)
    ],
    'CORS' =>[
      'allowedOrigins' => ['http://localhost', 'https://*.example.com'],
      'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
      'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
      'exposedHeaders' => ['Content-Encoding'],
      'maxAge' => 0
    ] 
  ]

  $app = new App($config);


$config =[
  "ENV" => "production"
]



// Get request
$app->get('/ping', function(Request $request) {
  return [
    'code' => 'OK',
    'message' => 'pong'
  ];
});

// Post request
$app->post('/blog', function(Request $request) {
  return $request->body();
});

// Put request
$app->put('/blog/:id', function(Request $request) {
  $param = $request->params()['id'];
  // do something with $param
});

// Delete request
$app->delete('/blog/:id', function(Request $request) {
  $param = $request->params()['id'];
  // do something with $param
});


$app->group(['prefix' => '/health'], 

  $app->get('/ping', function(Request $request) {
    return [
      'code' => 'OK',
      'message' => 'pong'
    ];
  })

  ,$app->get('/status', function(Request $request) {
    return [
      'code' => 'OK',
      'message' => 'pong'
    ];
  })
);


$middleware = function(Request $request, Closure $next) {
  echo "this is a middleware function";
  $next();
};

// The order of the parameters is not important
$app->get('/ping', $middleware, function(Request $request) {
  return [
    'code' => 'OK',
    'message' => 'pong'
  ];
});



$findUser = function(App $app, String $id){
  $app::dbConnect();     // Opens the connection with the database
    // Prepared statement
    $statement = $app()::db()->prepare(
      'SELECT * 
       FROM user 
       WHERE id = ?' 
    );
    $statement->bind_param('ss', $id);
    $statement->execute();
    $result = $statement->get_result()->fetch_all(MYSQLI_ASSOC);
  $app::dbExit();  // Closes the connection with te database
  return $result;
};


// Appending the module
$app::appendModule('logger', new Logger());

// Accessing the appended module 
$logger = $app:


// Use case
$app->get("/users", function(Request $request) {
  if(!validateToken($request->httpAuthorization))
    throw new UnauthorizedException("User is not logged in");
}); 
json
"scripts": {
        "start": [
            "Composer\\Config::disableProcessTimeout",
            "php -S localhost:9000 -t ./"
        ],
        "build": [
            "composer update --optimize-autoloader",
            "composer dump-autoload --optimize"
        ]
    },
    "config": {
        "optimize-autoloader": true
    }