Php Slim 3渲染方法无效

Php Slim 3渲染方法无效,php,slim,Php,Slim,我想在Slim3中进行简单的模板渲染,但出现错误: 这是我的密码: 名称空间控制器 class Hello { function __construct() { // Instantiate the app $settings = require __DIR__ . '/../../src/settings.php'; $this->app = new \Slim\App($settings); } public function

我想在Slim3中进行简单的模板渲染,但出现错误:

这是我的密码: 名称空间控制器

class Hello
{
    function __construct() {
// Instantiate the app
        $settings = require __DIR__ . '/../../src/settings.php';
        $this->app = new \Slim\App($settings);
    }

    public function index(){
        return $this->app->render('web/pages/hello.phtml');   //LINE20
    }
}
这是我得到的错误:

Message: Method render is not a valid method

App
对象本身不处理任何渲染,因此您需要一个模板附加组件,可能基于模板的
.phtml
扩展。使用composer安装:

composer require slim/php-view
然后,控制器方法将执行如下操作:

$view = new \Slim\Views\PhpRenderer('./web/pages');

return $view->render($response, '/hello.phtml');

您最终会希望将渲染器放在依赖项注入容器中,而不是在控制器方法中创建新实例,但这应该可以让您开始了。

我通过将渲染器放在容器中来处理这个问题。将其粘贴到主
index.php
文件中

$container = new \Slim\Container($configuration);
$app = new \Slim\App($container);
$container['renderer'] = new \Slim\Views\PhpRenderer("./web/pages");
然后在你的
Hello
类的文件中

class Hello
{
   protected $container;

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

   public function __invoke($request, $response, $args) {
        return $this->container->renderer->render($response, '/hello.php', $args);
   }
}

要清理此代码,请创建一个封装了此呈现逻辑的基本处理程序。

我得到了以下信息:
PHP注意:未定义的属性:controller\Hello::$view
PHP致命错误:对非对象调用成员函数render()
@smile22121我使示例代码更加明确,希望现在更清楚谢谢,但是$response中似乎有些错误:我得到了这个
PHP可捕获的致命错误:传递给Slim\Views\PhpRenderer::render()的参数1必须实现接口Psr\Http\Message\ResponseInterface,给定整数