Render view through controller using Slim Framework 3

Asked

Viewed 335 times

3

I’m trying to find a way to render the view using a controller on slim framework but for some reason I’m getting the error:

Message: Using $this when not in object context

The function is a public function and this within a class normally and I am following what it says in Documentation

My route:

$app->get('/home', '\App\Http\Controller\Teste::main');

The Base Controller:

class Controller
{
   protected $ci;

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

The test class:

class Teste extends Controller
{
   public function index($request, $response)
   {
      return "Hello!"; // Funciona
   }

   public function main($request, $response)
   {
       var_dump($this->ci); // Apresenta o erro
   }
}

1 answer

2

I have already lost many hours with a similar error. The problem is in this excerpt:

$app->get('/home', '\App\Http\Controller\Teste::main');

With the use of "::" you are calling a static method. And for that reason this does not exist. Try so:

$app->get('/home', '\App\Http\Controller\Teste:main');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.