sandesh phuyal
sandesh phuyal

Reputation: 111

Slim v3 and twig ( View Page displays page not found error)

I have installed slim framework 3 and twig template following the composer. When i call function http://localhost/elec/helloo/sandesh it displays Hello, Sandesh as followed on slim 3 documentation.

But when i try to call view page(inside templates folder).

It displays an error page Slim Application Error The application could not run because of the following error Error Description

Code Worked ( displays hello , {name} from function)

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
}); 

Code error ( displays error when called view page from function)

$settings =  [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];

$app = new Slim\App($settings);

// Get container
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    return new \Slim\Views\PhpRenderer("templates/");
};

// Render Twig template in route
$app->get('/helloo/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'view1.html', [
        'name' => $args['name']
    ]);
})->setName('profile');

Path Detail

elec>
    >>cache
    >>templates
               >>>view1.html
    >>vender
    >>.htaccess
    >>composer.json
    >>composer.lock
    >>index.php

Upvotes: 0

Views: 2611

Answers (2)

Chablis Bshara
Chablis Bshara

Reputation: 86

When passing the templates location, you have to provide a full path, (starting from the location of the running index.php file:

<?php
    $container['view'] = function ($container) {
        return new \Slim\Views\PhpRenderer(__DIR__ . "/../path/to/templates/");
    };

try it out, and good luck.

Note: I'm using the same line but with Twig render:

<?php
    $container['view'] = function ($container) {
        return new \Slim\Views\Twig(__DIR__ . "/../path/to/templates/");
    };

Upvotes: 1

sandesh phuyal
sandesh phuyal

Reputation: 111

$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true,
    ]
]);

// Calling twigview from controller
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('templates/views',[
        'cache' => false,
    ]);

    $view->addExtension(new \Slim\Views\TwigExtension(
        $container->router,
        $container->request->getUri()
    ));

    return $view;
};

$app->get('/home', function ($request, $response) {
    return $this->view->render($response, 'home.twig');
});

Upvotes: 0

Related Questions