# Why does error say Container requires arguments, but tutorial does not?

**URL:** <https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636>\
**Category:** Questions\
**Created:** [June 14, 2023, 3:25pm UTC](https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636 "2023-06-14T15:25:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anderoder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/anderoder/32/1546_2.png) [@anderoder](https://discourse.slimframework.com/u/anderoder)\
**Post date:** [June 14, 2023, 3:25pm UTC](https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636/1 "2023-06-14T15:25:30Z")

</div>

Hi!  
I’ve been trying to get the twig/view templating system to work on my new slim app. I got twig to work with the non-container based set up provided on the [Twig-View github page for slim](https://github.com/slimphp/Twig-View), but I figured I’d try to familiarize myself with how containers and dependency injection works. For some reason, though, I cannot for the life of me get the container example work. The error I get is:

```auto
Fatal error: Uncaught ArgumentCountError: Too few arguments to function DI\Container::__construct(), 0 passed in /var/www/html/mysite/public_html/index.php on line 16 and at least 2 expected in /var/www/html/mysite/vendor/php-di/php-di/src/DI/Container.php:77 Stack

```

I understand that php interpreter is complaining because I’m not passing in arguments to Container(), but I’m not sure what I should pass into Container as I’m initializing it because the tutorial/examples I’ve looked at never pass anything in. Is there some major misunderstanding I’m having regarding how this is supposed to work? Any help in understanding this would be greatly appreciated!

Example code from github for convenience:

```auto
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

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

// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Set view in Container
$container->set('view', function() {
    return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
});

// Create App
$app = AppFactory::create();

// Add Twig-View Middleware
$app->add(TwigMiddleware::createFromContainer($app));

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->get('view')->render($response, 'profile.html', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $str = $this->get('view')->fetchFromString(
        '<p>Hi, my name is {{ name }}.</p>',
        [
            'name' => $args['name']
        ]
    );
    $response->getBody()->write($str);
    return $response;
});

// Run app
$app->run();

```

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [June 14, 2023, 7:07pm UTC](https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636/2 "2023-06-14T19:07:36Z")

</div>

assuming you use PhpDI, try to require from your front controller (index.php) this example of a bootstrap file: (or put it directly in index.php, after `require '../vendor/autoload.php';` )

_bootstrap.php_

```php
<?php

declare(strict_types=1);

use DI\ContainerBuilder;
use Psr\Log\LoggerInterface;
use Slim\App;

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

    $definitions = array_merge(
        require './../config/settings/settings.php',
        require './../config/dependencies/dependencies.php'
    );
    $builder->addDefinitions($definitions);
    $container = $builder->build();

    $app = $container->get(App::class);

    require './../bootstrap/middleware.php';
    require './../bootstrap/routes.php';

    $app->run();

} catch (Throwable $t) {
}

```

---

<div class="post-metadata">

**Author:** ![anderoder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/anderoder/32/1546_2.png) [@anderoder](https://discourse.slimframework.com/u/anderoder)\
**Post date:** [June 14, 2023, 8:32pm UTC](https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636/3 "2023-06-14T20:32:56Z")

</div>

Thanks for the response!

I tried adding a bootstrap.php file with the code example you provided, but I didn’t yet have a settings.php or any of the other files your code example requires, so I made some dummy ones to see if that would work and it still produces the same error as before. I hadn’t read much about structuring files like this. Is it necessary to get the Container to run? Could you point in the direction of some documentation about best/required practices related to bootstrapping that might help me understand what’s going on?

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [June 15, 2023, 9:43am UTC](https://discourse.slimframework.com/t/why-does-error-say-container-requires-arguments-but-tutorial-does-not/5636/4 "2023-06-15T09:43:20Z")

</div>

How can you have the same error with my code if there is nothing like:

```php
DI\Container::__construct();

```

???

Just make your index.php like:

```php
<?php

declare(strict_types=1);

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

require './../bootstrap/bootstrap.php';

```

with a folder structure:

```auto
public
    index.php
bootstrap
    bootstrap.php
    routes.php
config
    settings
        settings.php
    dependencies
        dependencies.php
vendor

```

and there is no way, it throws any kind of exception you mentioned.  
Of course you can make any folder structure you want, if you don’t wish to have any values in settings.php just remove and leave only dependencies.php. It’s totally up to you.  
In relation to best bootstrapping practices, there is not such a thing. It depends on your personal preferences, your application and your perspective as a developer.  
In my case, I prefer to split the scope of bootstraping container, middlewares and routes declarations in separate files, apart of index.php. If you do everything at the begining in index.php, nobody is going to condemn you, but I can assure you after some time you will start to create your own, the most suitable folder structure for you.
