Closed garciaantonio20k closed 2 months ago
@garciaantonio20k I'm have the same problem now, and looking at the date of your post and no responses from the maintainers I assume this project it dead.
In your action, you're referencing $this->twig
.
Instead what you need to do is inject ContainerInterface
inside of your action and use the view
key that we automatically map to.
abstract class Action
{
protected $view;
public function __construct(LoggerInterface $logger, ContainerInterface $container)
{
$this->logger = $logger;
$this->view = $container->get('view');
}
}
Then in your action:
class IndexAction extends Action
{
protected function action(): Response
{
$viewData = [
'name' => 'Antonio'
];
return $this->view->render($this->response, 'index.twig', $viewData);
}
}
I had the same issue and will try this @l0gicgate thanks. It's a bit counterintuitive though and may benefit from being explained somewhere, on the README.md probably.
Injecting the DI container within the application should be avoided. In order to make use of proper autowiring, the DI container definition should be configured using the full interface or class name instead of "view".
use Slim\Views\Twig;
// ...
$container->set(Twig::class, function() {
return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
});
Within a class:
use Slim\Views\Twig;
// ...
final class HomeAction
{
private readonly Twig $twig;
public function __construct(Twig $twig)
{
$this->twig = $twig;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->twig->render($response, 'home.twig');
}
}
I'm closing this as resolved.
I am using the official slim4-skeleton. If I add Twig to my installation by following the steps in the documentation
Later i change the Action class
And with my IndexAction
I got the next error:
Entry "App\Application\Actions\Home\IndexAction" cannot be resolved: Entry "Slim\Views\Twig" cannot be resolved: Entry "Twig\Loader\LoaderInterface" cannot be resolved: the class is not instantiable
But if in the configuration i change only the name of key from "view" (the default) to Twig::class and $app->add(\Slim\Views\TwigMiddleware::createFromContainer($app)); to $app->add(\Slim\Views\TwigMiddleware::createFromContainer($app,Twig::class));
Everything works PERFECTLY
I don't why but the first step set('view') not add nothing to resolvedEntries in $container
Versions slim/slim 4.11.0 slim/twig-view 3.3.0 twig/twig 3.5.1 php-di/php-di 6.4.0