How extend Slim\App?

Hi, i want extend the slim\app class but i am getting this error message.

[07-Jun-2024 13:06:51 UTC] PHP Fatal error: Uncaught TypeError: DI\Definition\Source\DefinitionFile::{closure}(): Argument #1 ($app) must be of type Ultra\Foundation\App, Slim\App given, called in /home/myphp/public_html/myph/config/container.php on line 18 and defined in /home/myphp/public_html/myphp/routes/web.php:12
Stack trace:

This is what i have tried.



namespace Ultra\Foundation;

use Slim\App;
//use Slim\Factory\AppFactory;

class App extends Slim\App

i don’t know what i am doing wrong,
i need help.

Somewhere in you application you are passing a Slim\App instance instead of your custom App instance.

Check the file container.php on line 18 to see where it is coming from.

I guess it’s a incorrect using statement, because the classname is App in both cases.

Thanks, i will check and get back to you.

Still not working.
this is my container.php



use Psr\Container\ContainerInterface;
use Ultra\Foundation\App;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;

return [
    'settings' => function () {
        return require __DIR__ . '/../config/settings.php';

App::class => function (ContainerInterface $container) {
    $app = AppFactory::createFromContainer($container);

    // Require routes
    (require __DIR__ . '/../routes/web.php')($app);

    //Require middlewares
    (require __DIR__ . '/../app/Http/Middlewares/middleware.php')($app);

      return $app;

   Twig::class => function (ContainerInterface $container): Twig {
        /** @var array<string, array<string, mixed>> $settings */
        $settings = $container->get('settings');
        $options = [
            'debug' => $settings['app']['debug'],
            'cache' => $settings['view']['cache'],

        /** @var string $path */
        $path = $settings['view']['path'];

        $twig = Twig::create($path, $options);

        /** @var Translator $translator */
        //$translator = $container->get(Translator::class);

        //$twig->getEnvironment()->addGlobal('locale', $translator->getLocale());
        //$twig->addExtension(new TranslationExtension($translator));

        return $twig;


and this is my route/web.php



use App\Controllers\AboutController;
use App\Controllers\ContactController;
use App\Http\Controllers\IndexController;

return static function (Ultra\Foundation\App $app): void {
    $app->get('/', [IndexController::class, 'index'])->setName('home.index');
    $app->get('/about', [AboutController::class, 'index'])->setName('about.index');
    $app->get('/contact', [ContactController::class, 'index'])->setName('contact.index');

Please take a look. thanks

In container.php, you are using Slim’s AppFactory here:

$app = AppFactory::createFromContainer($container);

This returns in instance of Slim\App as you can see here: Slim/Slim/Factory/AppFactory.php at 4.x · slimphp/Slim · GitHub.

You will need your own AppFactory.

1 Like