Search code examples
phpcontainersslim-3

Error: Slim instance must be Slim\Views\Twig, Slim\Container given


I'm trying basic container tutorials on Youtube, and I'm stuck on this part. I made sure that a twig instance is given and not a container, but the error still appears.

heres the container part of index.php:

$container['view'] = function ($container)
    {
    $view = new \Slim\Views\Twig('src/Views', ['cache' => false,]);
    $view->addExtension(new \Slim\Views\TwigExtension(
    $container->router,
    $container->request->getUri()
    ));

    return $view;
};

$container['loginController'] = function ($container){

    return new \App\Controllers\LoginController($container->view);
};

routes.php:

$app->get('/', 'LoginController:login_redirect');

controller.php:

 use Slim\Views\Twig as View;

protected $view;

public function __construct(View $view){
    $this->view = $view;

}

public function login_redirect($request, $response){
    $this->view->render($response, 'testing.twig');

}

Solution

  • Shouldn't $container['view'] be passed as constructor argument here, instead of $container->view?

    $container['loginController'] = function ($container){
        return new \App\Controllers\LoginController($container->view);
    };