Grand Master
Grand Master

Reputation: 11

slim framework 3, fail to render html page

Am using twig-view to render the html, currently am unable to change the Content type returned to the browser, i can see that content type returned by slim as json instead of html content hence all codes are displayed in browser

 $app->get('/',function(Request $req,Response $res,array $args) use ($app){
        $container = $app->getContainer();
        $container->view->render($res, 'test.html',[]);
    });

enter image description here

Upvotes: 0

Views: 444

Answers (2)

Grand Master
Grand Master

Reputation: 11

Addition to Zamrony P. Juhara i have found that middleware i put was editing response to be returned as json content ->withHeader("Content-Type", "application/json")

/*
CORS
*/
$app->add(function ($req, $res, $next) {
    $response = $next($req, $res);
    return $response
            //->withHeader("Content-Type", "application/json")
            //->withHeader('Access-Control-Allow-Origin', 'http://localhost:2222/')
            ->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,PATCH,OPTIONS');
}); 

so the browser was only getting json content response hence it was dumping all codes as json content instead of Content-Type: "text/html", and that solved my problem

Upvotes: 0

odan
odan

Reputation: 4952

Try to return the response like this:

return $container->view->render($res, 'test.html', []);

Upvotes: 1

Related Questions