Error "not a class nor a valid container entry"

So basically I have this controller:

UserEntryController.php

<?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

Please help me solve the problem.

<?php

use Slim\App;
use App\Controllers\UserEntryController;

return function (App $app)
{
    $app->post('/create-user', UserEntryController::class . ':createGuest');
};

Checked for Slim4 and phpDI :smile: and perhaps adapt route to your action name.

Oho, thank you for a fast reply!
Tried changing to your code, but I get this error instead:

‘App\Controllers\UserEntryController:createGuest’ is neither a callable nor a
valid container entry

Sorry, I am new to Slim, started it yesterday so I might not see some basic mistakes :sweat_smile:

also there are double slashes in this message, like App \ Controllers \ UserEntryContr

Show me your composer.json and index.php files. :slightly_smiling_face:

composer.json

{

    "require": {

        "slim/psr7": "^1.2",

        "slim/slim": "4.*",

        "php-di/php-di": "^6.3",

        "php-di/slim-bridge": "^3.0",

        "respect/validation": "^2.1",

        "tuupola/slim-jwt-auth": "^3.5",

        "firebase/php-jwt": "^5.2",

        "illuminate/database": "^8.16"

    },

    "autoload": {

        "psr-4": {

            "App\\": "app/"

        }

    }

}

index.php

<?php
require_once '../config/bootstrap.php';

and bootstrap.php

<?php

use DI\Container;

use DI\Bridge\Slim\Bridge as SlimAppFactory;

require_once '../../vendor/autoload.php';

$container = new Container();

$settings = require_once 'settings.php';

$settings($container);

$app = SlimAppFactory::create($container);

$middleware = require_once 'middleware.php';

$middleware($app);

$routes = require_once 'routes.php';

$routes($app);

$app->run();

No problems :smiley:

Lets try from the beginning. First start a new project with this json:

{
    "require": {
        "slim/slim": "4.*",
        "guzzlehttp/psr7": "^1.6",
        "http-interop/http-factory-guzzle": "^1.0",
        "php-di/slim-bridge": "^3.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}
1 Like

Than within your:

project/public/index.php:

<?php

require '../vendor/autoload.php';

use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use App\Controllers\UserEntryController;

try {
    //$definitions = .........  //if you have any manual
    $builder = new ContainerBuilder();
    $builder->useAutowiring(true);
    $builder->useAnnotations(true);
    // $builder->addDefinitions($definitions); \\if you have any manual

    $container = $builder->build();

    AppFactory::setContainer($container);
    $app = AppFactory::create();

    $app->addBodyParsingMiddleware();
    $app->addRoutingMiddleware();

    $app->post('/create-user', UserEntryController::class . ':createGuest');

    $app->run();

} catch (Exception $e) {
    echo $e->getMessage();
}

That should be at least some starting point.

Did it and it got me to:

Slim\Exception\HttpNotFoundException

Code: 404

Change your path to GET:

$app->get('/create-user', UserEntryController::class . ':createGuest');

From within your project folder start developer server for simplicity:

php -S 127.0.0.1:8080 -t public/

Than in your browser

127.0.0.1:8080/create-user

[Mon Nov 30 17:16:58 2020] 127.0.0.1:63575 Accepted
[Mon Nov 30 17:16:58 2020] 127.0.0.1:63576 Accepted
[Mon Nov 30 17:16:58 2020] 127.0.0.1:63575 [200]: GET /create-user
[Mon Nov 30 17:16:58 2020] 127.0.0.1:63575 Closing

Output in browser: Callable App\Controllers\UserEntryController does not exist

change your route:

// 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;
    }
}

Actually the above controller should work with no problems with the initial route either:

    $app->get('/create-user', UserEntryController::class . ':createGuest');

As the final thoght you had overmanipulated your response within controller - I should have put more attention on that at the beginning, shame on me :smiley: .

I changed the controler and so I tried with the changed route and I got this mess:

No entry or class found for 'App\Controllers\UserEntryController’

and with the unchanged route there is this output

Callable App\Controllers\UserEntryController does not exist

I don’t know which one of these is easier to deal with :smiley:

You got me :face_with_raised_eyebrow:
Just for this case I created a project with exactly:

my_project/composer.json

{
  "require": {
    "slim/slim": "4.*",
    "guzzlehttp/psr7": "^1.6",
    "http-interop/http-factory-guzzle": "^1.0",
    "php-di/slim-bridge": "^3.0"
  },
  "autoload": {
    "psr-4": {
      "App\\": "app/"
    }
  }
}

my_project/public/index.php

<?php

declare(strict_types=1);

require '../vendor/autoload.php';

use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use App\Controllers\UserEntryController;

try {
    $builder = new ContainerBuilder();
    $builder->useAutowiring(true);
    $builder->useAnnotations(false);

    $container = $builder->build();

    AppFactory::setContainer($container);
    $app = AppFactory::create();

    $app->addBodyParsingMiddleware();
    $app->addRoutingMiddleware();

    $app->get('/create-user', UserEntryController::class . ':createGuest');

    $app->run();

} catch (Exception $e) {
    echo $e->getMessage();
}

my_project/app/Controllers/UserEntryController.php

<?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.

Welcome, @leejohny653!
Please create a new topic with all the details, code examples and the exact error message. Thanks.

Okay, Thanks.

thanks dude, it works for me with php 8.0.10 :metal:

u save my life :laughing: