Hello, first of all excellent work with this project, it is very clean and explanatory. I have created the following code in Router.php to be able to make POST requests, however I don't know if it is the correct way to do it:
`class Router
{
public function __invoke(RouteCollection $routes)
{
$messageError = null;
$auth = new Auth();
$context = new RequestContext();
$request = Request::createFromGlobals();
$context->fromRequest(Request::createFromGlobals());
$matcher = new UrlMatcher($routes, $context);
try {
$matcher = $matcher->match($context->getPathInfo());
array_walk($matcher, function(&$param)
{
if(is_numeric($param))
{
$param = (int) $param;
}
});
$controller = $matcher["controller"][0];
$action = $matcher["controller"][1];
$controllerInstance = new $controller();
if($context->getMethod() === $matcher["methods"][0]){
if(isset($matcher["condition"])){
if($matcher["condition"] === "token"){
if(!$auth->checkToken($request->get('token'))){
throw new SessionNotFoundException("unauthorized");
}
}
}
if($matcher["methods"][0]==="GET")
$params = array_merge(array_slice($matcher, 2, -1), array('routes' => $routes));
else
$params = array_merge(array($request), array('routes' => $routes));
}
else{
throw new MethodNotAllowedException(array());
}
call_user_func_array(array($controllerInstance, $action), $params);
} catch (MethodNotAllowedException $e) {
$messageError = 'Route method is not allowed.';
} catch (ResourceNotFoundException $e) {
$messageError = 'Route does not exists.';
} catch (NoConfigurationException $e) {
$messageError = 'Configuration does not exists.';
} catch (SessionNotFoundException $e) {
$messageError = 'Session not found, unauthorized';
}
if(isset($messageError)){
$aceResponse = new AceResponse();
$aceResponse->setResponse("0" ,array("error"=>$messageError));
$response = new Response();
$response->setContent(json_encode($aceResponse->getResponse()));
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'application/json');
$response->send();
}
}
}
// Invoke
$router = new Router();
$router($routes);
My web.php
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use App\Controllers\SkinWebController;
$routes = new RouteCollection();
$routes->add('SKIN',
new Route(constant('URL_SUBFOLDER') . '/skin/{request}',
array('controller' => [SkinWebController::class, 'showViewAction'],
'methods'=>['GET']),
array('request' => '[a-zA-Z0-9-]+')));
$routes->add('GET_SKIN_THEME',
new Route(constant('URL_SUBFOLDER') . '/ace/getSkinTheme',
array('controller' => [SkinWebController::class, 'getSkinThemeAction'],
'methods'=>['POST'], 'condition'=>'token'),
array()));`
And my SkinWebController.php
`class SkinWebController
{
public function showViewAction(string $request, RouteCollection $routes)
{
$skin = new SkinWeb();
$auth = new Auth();
$skin->setFormToken($auth->generateToken());
$skin->setIdRequest($request);
$skin->setUrl(constant("API_PHP"));
require_once APP_ROOT . '/views/skinWeb.php';
}
public function getSkinThemeAction(Request $request, RouteCollection $routes)
{
$response = new Response();
$api = new AceWebMethods();
$payload = json_decode($request->get('payload'));
$result = $api->getSkinTheme($payload);
$response->setContent(json_encode($result->getResponse()));
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'application/json');
$response->send();
}
}`
How can I configure to send json data to the api ?
Actually can only send form-data type. Thanks very much.