Slim Application Error - View cannot render

Hi,

I’m new with Slim and I’m trying to do the first aplication tutorial but i have an error.

They say me: View cannot render tickets.phtml because the template does not exist

In index.php i put this to set template directory:

$container['view'] = new \Slim\Views\PhpRenderer("../templates/");

And this is my folder structure:

/project
    /src
        /public
           index.php
       /templates
           tickets.phtml

What I’m doing wrong?

Thanks,

Try single quotes around the directory?

$container['view'] = new \Slim\Views\PhpRenderer('../templates/');

Otherwise, what I see there looks good.

I try with single quotes and nothing, continue with same error.

I’m stay debugging and I see in /vendor/slim/php-view/src/PhpRenderer.php in line 153 this:

if (!is_file($this->templatePath . $template)) {
        throw new \RuntimeException("View cannot render `$template` because the template does not exist");
}

The problem is that $this->templatePath . $template is this ‘’…/templates/ticketadd.phtml" and the is_file function return false, so !false its true and throw that exception.

What do you thing was the problem?

Is the logger successfully logging to /logs/app.log?

Yes, the logger is logging successfully.

This is my logger:

$container['logger'] = function($c) {
    $logger = new \Monolog\Logger('Slim_logger');
    $file_handler = new \Monolog\Handler\StreamHandler("../logs/app.log");
    $logger->pushHandler($file_handler);
    return $logger;
};

And here is the folder:

/project
   /logs
       app.log
   /src
       /public
          index.php
      /templates
          tickets.phtml

in order if I use $container[‘renderer’] instead of $container[‘view’] like this works well:

use Slim\Views\PhpRenderer;
$container['renderer'] = new PhpRenderer("./templates");
$app->get('/tickets', function (Request $request, Response $response) {
    $mapper = new TicketMapper($this->db);
    $tickets = $mapper->getTickets();
    $response = $this->renderer->render($response, "tickets.phtml", ["tickets" => $tickets]);
    return $response;
}

I think it is because you used "./templates" instead of ../templates/. It seems ./templates is correct, which is strange because index.php is in /public :confused:

You can use __DIR__ for a path relative to the directory of the current file, so you point to a directory regardless of the current directory of the main PHP script. If you use

$container['view'] = new \Slim\Views\PhpRenderer(__DIR__ . '/../templates/');

in project/public/index.php, then it should correctly point to the /project/templates/ directory.