Reuqest object null when passed

This question may need to go to a different forum but I’ll ask here just in case. I am trying to pass the Request object to the constructor of a base class. In the main index file it shows the variable as valid but when I pass it to the base class constructor it says that the object is NULL. So the obvious question is what am I doing wrong? but also should I be using something in the framework to accomplish this same behavior (routing ?)

Here is the abbreviated code

(Index.php)

$app->post(’/Service’, function (Request $request, Response $response) {

$baseObj = new BaseEntity($request, $response);

return $response;

});

(BaseEntity.php)

class BaseEntity {
public function __construct(Request $req, Response $resp) {
$this->request = $req;
$this->response = $resp;
$this->rawjson = $req->getParsedBody();
}

            private $request;
            private $response;
            private $rawjson;

}

and this is the error I receive

Type: TypeError
Message: Argument 1 passed to BaseEntity::__construct() must be an instance of Request, instance of Slim\Http\Request given,

Since you are type hinting Request you will need to say where to find it in BaseEntity.php. (And then it won’t know what Response is after that is fixed, so you will likely want to add that too.)

<?php

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class BaseEntity
{
    // ...
}

Now I get this error:

Type: TypeError
Message: Argument 1 passed to BaseEntity::__construct() must implement interface Psr\Http\Message\ServerRequestInterface, none given,

Someone else might be able to infer what is going on, but I’d need to see a bit more of your code and/or your setup as Slim\Http\Request does implement ServerRequestInterface, which from the previous error does seem to be the object passed.

////////////////////////////////////////////////////////////
index.php
///////////////////////////////////////////////////////////

<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require '../vendor/autoload.php'; require_once 'utility_functions.php'; require_once 'Endpoint/BaseEntity.php' $app = new \Slim\App; $app->post('/Service', function (Request $request, Response $response) { $baseObj = new BaseEntity($request, $response); return $response; }); $app->post('/Service2', function (Request $request, Response $response) { }); $app->post('/Service3', function (Request $request, Response $response) { }); $app->run(); ?>

/////////////////////////////////////////////////////////////////////////////
BaseEntity.php
/////////////////////////////////////////////////////////////////////////////

<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; class BaseEntity { public function __construct(Request $req, Response $resp) { $this->request = $req; $this->response = $resp; $this->rawjson = $req->getParsedBody(); } private $request; private $response; private $rawjson; } ?>

////////////////////////////////////////////////////////
File Structure
//////////////////////////////////////////////////////

~/project/src
composer.json
composer.lock
vendor

~/project/src/public/
index.php
utility_functions.php

~/project/src/public/Endpoint/
BaseEntity.php