slimphp / Twig-View

Slim Framework view helper built on top of the Twig templating component
http://slimframework.com
MIT License
356 stars 86 forks source link

Error using Twig with Slim 4 & Container #285

Closed garciaantonio20k closed 2 months ago

garciaantonio20k commented 1 year ago

I am using the official slim4-skeleton. If I add Twig to my installation by following the steps in the documentation

$container->set('view', function() {
    return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
});
$app->add(\Slim\Views\TwigMiddleware::createFromContainer($app));

Later i change the Action class

  protected $twig;

    public function __construct(LoggerInterface $logger, Twig $twig)
    {
        $this->logger = $logger;
        $this->twig = $twig;
    }

And with my IndexAction

class IndexAction extends Action
{

    protected function action(): Response
    {
        $viewData = [
            'name' => 'Antonio'
        ];
        return $this->twig->render($this->response, 'index.twig', $viewData);
    }
}

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

plasid commented 12 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.

l0gicgate commented 11 months ago

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);
    }
}
Rakjlou commented 8 months ago

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.

odan commented 2 months ago

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.