Hello guys,
I already read through the net and tried to find a solution for my problem. I also posted this to the issue section on git before I found this. Sorry about that. You can delete the git-post.
First I want to say that I have been building a REST API using the slim framework.
I just want to give you an example (which is working fine):
$app->post('/login', function ($request, $response, $args) {
$response_array = array(
"status" => "sucess",
"data" => array(),
"message" => null
);
$parsedBody = $request->getParsedBody();
$validate = new Validate();
$validation = $validate->check($parsedBody, array(
'username' => array(
'required' => true,
),
'password' => array(
'required' => true,
'min' => 10
)
)
);
if($validation->passed()){
echo 'working';
}else{
$response->write(json_encode($response_array));
}
return $response;
});
Well this is working fine while sending a JSON object to “/login”. But my index.php is getting bigger and bigger now, so I decided start using controllers by creating an abstract class which represents the main controller interface.
$app->get('/', \Routes\RootController::class);
$app->post('/login', \Routes\LoginController::class);
<?php
namespace Routes;
use Interop\Container\ContainerInterface;
abstract class MainController
{
protected $ci;
public function __construct(ContainerInterface $ci)
{
$this->ci = $ci;
}
abstract public function __invoke($request, $response, $args);
}
Then I created the LoginController.
<?php
namespace Routes;
class LoginController extends MainController
{
public function __invoke($request, $response, $args)
{
$response_array = array(
"status" => "sucess",
"data" => array(),
"message" => null
);
$parsedBody = $request->getParsedBody();
$validate = new \Validate();
$validation = $validate->check($parsedBody, array(
'username' => array(
'required' => true,
),
'password' => array(
'required' => true,
'min' => 10
)
)
);
if($validation->passed()){
echo 'working';
}else{
$response->write(json_encode($response_array));
}
return $response;
}
}
And here the problem came up. It seems that the “$request” is empty or null. Also by checking it with:
if(!$request){
echo 'not null';
}
Really thanks a lot for your time!!!