Twig View - Slim4

Hi All,

  Request your help in setting up Twig, We are trying to migrate our application from Slim 3.x to Slim 4.x , hence all these issues and request your guidance.

container.php

<?php

use Slim\App;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Selective\Config\Configuration;

return [
    Configuration::class => function () {
        return new Configuration(require __DIR__ . '/settings.php');
    },

    App::class => function (ContainerInterface $container) {
        AppFactory::setContainer($container);
        $app = AppFactory::create();
        return $app;
    },
    TwigMiddleware::class => function (ContainerInterface $container) {
        return TwigMiddleware::createFromContainer($container->get(App::class), Twig::class);
    },

    Twig::class => function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);
        $settings = $config->getArray('twig');
        $options = $settings['options'];
        $options['cache'] = $options['cache_enabled'] ? $options['cache_path'] : false;
        $twig = Twig::create($settings['paths'], $options); 
        return $twig;
  },
];

?>

middleware

<?php

use Slim\App;
use Slim\Views\TwigMiddleware;
use Selective\Config\Configuration;
use Selective\BasePath\BasePathMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\Middleware\ErrorMiddleware;

return function (App $app) {
       $app->addRoutingMiddleware();
       $app->add(TwigMiddleware::class);
       $app->add(BasePathMiddleware::class);
       $app->add(ErrorMiddleware::class);
};
?>

routes.php

<?php
use Slim\App;
use Slim\Routing\RouteContext;
use Slim\Routing\RouteCollectorProxy;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Views\Twig;

return function(App $app) {
################################################################
# Page 1.0
################################################################
$app->get('/', function($request, $response)  {
	  return $this->$view->render($response, 'page_dashboard.html');
})->setName('root');

$app->get('/login', function($request, $response)  {
      return $this->$view->render($response, 'page_login.html');
})->setName('login');

};

?>

Hello Vino, I think this post can give some help

You should provide more details, e.g. the error message.

This line is not valid:

Within class methods non-static properties may be accessed by using -> (Object Operator):

return $this->view->render($response, 'page_dashboard.html');

https://www.php.net/manual/en/language.oop5.properties.php

You can use PhpStorm, a linter and phpstan to check your code.

Hi Odan,

Thank you very much, and sorry for not providing more information.