I just started using slim4 framework for REST API. It’s a big project, so i want to distrubed codes into multiple controllers. When I use GET method then it is working, but if i use POST then it’s giving 405 - method not allowed. Guide me how to use it for post method.
Index.php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\AppFactory;
use Slim\Exception\NotFoundException;
use Slim\Exception\HttpMethodNotAllowedException;include ‘vendor/autoload.php’;
global $app;
$app->post(‘/auth/getotp’, \VTEL\Authentication\AuthController::class . ‘:getOTP’);$app = AppFactory::create();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
AuthController.php
namespace VTEL\Authentication;
use Psr\Container\ContainerInterface as ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;class AuthController
{
protected $version;
protected $container;public function __construct() { $this->version="1.0"; } public function getVersion() { return array('name'=>'AuthController', 'version'=>$this->version); } public function getOTP(Request $request, Response $response, $args) { $vt_genotp_postdata = empty($request->getParsedBody())?array():$request->getParsedBody(); $resoutput = json_encode(array('code'=>0,'message'=>'Success','data'=>$vt_genotp_postdata), JSON_PRETTY_PRINT); return $response->withStatus(200)->getBody()->write($resoutput)->withHeader('Content-Type', 'application/json'); }
}