<?php
namespace App\Controllers;
use App\Requests\CustomRequestHandler;
use App\Response\CustomResponse;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\RequestInterface as Request;
class UserEntryController
{
protected $customResponse;
public function __construct()
{
$this->customResponse = new CustomResponse();
}
public function createGuest(Request $request, Response $response)
{
$username = CustomRequestHandler::getParam($request, "name");
$responseMessage = "this route works";
return $this->customResponse->is200Response($response, $username);
}
}
and then I call it here:
routes.php
<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App;
use App\Controllers\UserEntryController;
return function (App $app)
{
$app->post('/create-user',[UserEntryController::class,'createGuest']);
};
and in the end I get this error: Cannot call createGuest() on App\Controllers\UserEntryController because it is not a class nor a valid container entry
<?php
use Slim\App;
use App\Controllers\UserEntryController;
return function (App $app)
{
$app->post('/create-user', UserEntryController::class . ':createGuest');
};
Checked for Slim4 and phpDI and perhaps adapt route to your action name.
// add at the top of index.php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
// change a route a bit:
$app->get('/create-user', function (Request $request, Response $response, array $args) {
return $this->get(UserEntryController::class)->createGuest($request, $response);
});
Try this controller as a starting point:
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class UserEntryController
{
public function createGuest(Request $request, Response $response): Response
{
$response->getBody()->write('Hello World');
return $response;
}
}
<?php
declare(strict_types=1);
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class UserEntryController
{
public function createGuest(Request $request, Response $response): Response
{
$response->getBody()->write('Hello World');
return $response;
}
}
with php 7.3
and it works like a charm on dev server:
php -S 127.0.0.1:8080 -t public/
Have not a clue why it doesnât in your case.
Are you sure youâve got identical files content ?
Damm even I am having a similar kind of issue, I have searched all over the internet and even have posted on number of threads on different forum, no solution seems to work. I am really frustrated, can anyone of you here help me resolve this issue, I am very much tired now.