1. Go to this page and download the library: Download webx/routes 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/ */
webx / routes example snippets
use WebX\Routes\Api\RoutesBootstrap;
use WebX\Routes\Api\Response;
eJson([
"user" =>
["name" => "Mr. Andersson"]
]
]);
$response->data(1998,"user.popular"); //Merges the data into 'user.popular'
});
use MyBusiness\Api\Services\IAdminService;
class AdminService implements IAdminService {
public function __construct() {}
public function countAdmins() {
return 3;
}
}
use WebX\Routes\Api\RoutesBootstrap;
use WebX\Routes\Api\Routes;
use WebX\Routes\Api\Response;
use MyBusiness\Api\Services\IAdminService;
RoutesBootstrap::run(function(Routes $routes) {
$routes->onSegment("admin",[function(Response $response, IAdminService $adminService) {
$response->data($adminService->countAdmins(),"count");
},"admin"]);
// The admin-configuration is only loaded if routes matched the `admin` segment.
});
return [];
return [
"ioc" => [
"register" => [ // The classes will be scanned for all their implemented interfaces.
[MyClass:class],
[MyOtherClass:class]
],
"initStatic" => [ //
[MyValueObject::class,"initMethod"] // The static "initMethod" will be invoked with declared dependencies.
]
],
"mappings" => [
"closureParameterName" => "iocInstanceId"
]
]
use WebX\Routes\Api\RoutesBootstrap;
use WebX\Routes\Api\Routes;
RoutesBootstrap::run(function(Routes $routes) {
$routes->onSegment("admin",['MyBusiness\\Controllers\\AdminCtrl','adminConfig']
// The admin-configuration is only loaded if routes matched the `admin` segment. Methods on the
// controller will automatically be mapped by the next available segment
// If no next segment exist Routes will map the request to `index()` of the controller instance
});
namespace MyBusiness\Controllers;
class AdminController {
private $logService;
public function __construct(ILogService $logService) {
$this->logService = $logService;
}
public function countAdmins(Response $response, RawResponseType $responseType, IAdminService $adminService) {
$response->type($responseType);
$response->data("Hello there " + $adminService->countAdmins() + " admin(s)");
}
}
#Controller functions can be invoked with user parameters. Parameters, taking precedence over IOC injected ones,
#can be defined in the last arguemnt `$parameters` array or with parametes defiend in the `onMatch` switch.