Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim 3 render method not valid

Tags:

php

slim

I want to make simple template rendering in Slim3 but I get an error:

Here is my code : namespace controller;

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
    }
}

This is the error I get :

Message: Method render is not a valid method
like image 646
smile 22121 Avatar asked Feb 17 '26 01:02

smile 22121


1 Answers

The App object doesn't handle any rendering on its own, you'll need a template add-on for that, probably this one based on your template's .phtml extension. Install with composer:

composer require slim/php-view

Then your controller method will do something like this:

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

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

You'll eventually want to put the renderer in the dependency injection container instead of creating a new instance in your controller method, but this should get you started.

like image 82
mzulch Avatar answered Feb 18 '26 13:02

mzulch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!