Slim controller issue : must be an instance of ContainerInterface, instance of Slim\\Container given

Slim controller issue : must be an instance of ContainerInterface, instance of Slim\\Container given

__construct must be an instance of slim views twig instance of slim container given
slim 4
slim dependency injection
slim framework
slim pass container to controller
slim get app instance
slim 4 routes
slim 4 example

I am trying to use controller in Slim however keep getting the error

PHP Catchable fatal error: Argument 1 passed to TopPageController::__construct() must be an instance of ContainerInterface, instance of Slim\Container given

My index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

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

spl_autoload_register(function ($classname) {
    require ("../classes/" . $classname . ".php");
});

$app = new \Slim\App(["settings" => $config]);
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write("Welcome");
    return $response;
});
$app->get('/method1', '\TopPageController:method1');
$app->run();
?>

My TopPageController.php

<?php
class TopPageController {
   protected $ci;
   //Constructor
   public function __construct(ContainerInterface $ci) {
       $this->ci = $ci;
   }

   public function method1($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome1");
        return $response;
   }

   public function method2($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome2");
        return $response;
   }

   public function method3($request, $response, $args) {
        //your code
        //to access items in the container... $this->ci->get('');
        $response->getBody()->write("Welcome3");
        return $response;
   }
}
?>

Thanks. I am using Slim 3.


Your code seems to be based on the Slim 3 documentation at http://www.slimframework.com/docs/objects/router.html which does not contain all the required code to avoid the Exception being thrown.

You basically have two options to make it work.

Option 1:

Import the namespace in your index.php, just like it is being done for Request and Response:

use \Interop\Container\ContainerInterface as ContainerInterface;

Option 2:

Change the constructor of the TopPageController to:

public function __construct(Interop\Container\ContainerInterface $ci) {
    $this->ci = $ci;
}

TL;DR

The reason the Exception is thrown is that the Slim\Container class is using the Interop\Container\ContainerInterface interface (see the source code):

use Interop\Container\ContainerInterface;

Since Slim\Container is extending Pimple\Container, the following should all be valid (working) type declarations for your controller's method:

public function __construct(Pimple\Container $ci) {
    $this->ci = $ci;
}

...or even...

public function __construct(ArrayAccess $ci) {
    $this->ci = $ci;
}

PHP Fatal Error Facade Container - Installation, Controller::__construct() must be an instance of Interop\Container\ ContainerInterface, instance of Slim\Container given, called in� Controller::__construct() must be an instance of Interop\Container\Co… Resolved by replacing this in all my files … use Interop\Container\ContainerInterface with these …


Based on a later change in Slim3 (from version 3.12.2 to 3.12.3) a slightly different ContainerInterface is required. This changes \Interop\ to \Psr\. Add the following on top of you code or change the existing use:

use Psr\Container\ContainerInterface;

Or change the constructor:

public function __construct(\Psr\Container\ContainerInterface $container)
{
    $this->container = $container;
}

Facing issue when passing ContainerInterface in , Argument 1 passed to OurOwnNamespace\Controller\Test::__construct() must be an instance of Psr\Container\ContainerInterface, null given,� The problem is I am passing an instance of Slim\Views\Twig - at least I think I am. I've not used Slim in a few months so I might be missing something obvious. I've not used Slim in a few months so I might be missing something obvious.


Just paste below code in your controller

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Interop\Container\ContainerInterface as ContainerInterface;

Your controller's construct should be looking like below

public function __construct(ContainerInterface $container) {
        parent::__construct($container);

    }

I think you your are making mistake in giving namepspace in controller for ContainerInterface.

Dependency Container, You don't have to provide a dependency container. If you do, however, You can fetch an explicit reference to the container instance from inside a Slim application route like this: If you use Slim's built-in container, these are provided for you. I am using hhvm extensions api to try and build predis as a php only extension at the time of building HHVM. The purpose is to hopefully get better performance from it.


When I use v2.0 with slim/twig have some problem. � Issue #41 , I just follow your doc on http://php-di.org/doc/frameworks/slim.html but I get this must implement interface Interop\Container\ContainerInterface, instance of interface Interop\Container\ContainerInterface, instance of DI\Container given you need to replace it with Psr\Container\ContainerInterface . Why GitHub? Features →. Code review; Project management; Integrations; Actions; Packages; Security


Instance of Slim\Container where Interop\Container , Instance of Slim\Container where Interop\Container\ContainerInterface was given #21. Open. kacperaniolek opened this issue on Mar 16 � 0 comments � May be fixed by #22. Open must be an instance of Interop\Container\ ContainerInterface, instance of Slim\Container use Psr\Container\ ContainerInterface #22. Open. For things such as the flip-flops, nodes and in Figure 1, the microcontroller must instance, the width of a bus can be machine states necessary for the perform its tasks using a link to the enlarged in a wink, while performing design must be declared.


PHP Slim Framework Create Controller, Slim controller issue : must be an instance of ContainerInterface, instance of Slim \\Container given. I am trying to use controller in Slim however keep getting the�