Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IIS doesn't recognise PHP tags for 'simple' Laravel routes only

There are plenty of similar questions out there, but none that I’ve found resolve my specific problem. I’m quite sure it’s a configuration in my local IIS server, but I haven’t been able to work it out myself. FYI, my local dev machine is Windows 11, and I’m running IIS 10.

I have the following simple code in my Laravel routes/web.php file:

Route::get('/info', function() { return '<!DOCTYPE html>
    <html><head><title>Testing</title></head>
    <body>
        <p>Hello from simple route</p>
        <p><?php
            echo phpinfo();
        ?></p>
    </body></html>'; });
Route::get('/info2', function() { return view('info'); });

Then I have the resources/views/info.blade.php file that looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<!DOCTYPE html>
<html>
<head>
    <title>Info Test 2</title>
</head>
<body>
    <p>Hello from Blade Template</p>
    <p><?php
        echo phpinfo();
    ?></p>
</body>
</html>

When I access http://myserver/info2 in the browser, I get a nicely formatted PHP-info page as one would expect. This proves that PHP is running correctly on my IIS setup.

But when I access http://myserver/info, I only get text from paragraph 1 of the HTML document, saying "Hello from simple route". PHP-info is not rendered, and looking at the dev-tools output we can see that the <?php tags are commented out:

enter image description here

I understand that this is because IIS isn’t recognising that it it dealing with a *.php file, and therefore doesn’t realise that it should pass the response through its PHP parser. But what I don’t understand is why it doesn’t work only for "simple" Laravel routes compared with Blade templates. (If I understood why, I’d probably be able to work out which IIS config to change to fix it).

Under the IIS server "Handler Mappings", I have the following setup:

enter image description here

I’ve tried setting up a *.html and a * path to the above config, so that it doesn’t always look for a PHP file to call the FastCGI/PHP processor, but none of this has worked. Can anyone help point me in the right direction?

>Solution :

When you return in /info a string, it will not be parsed as a file like resources/views/info.blade.php.
It will be just returned as string.
So <?php ?> will not be parsed by PHP.

This will work:

Route::get('/info', function() { 

ob_start();
phpinfo();
$phpinfo = ob_get_clean();

return '<!DOCTYPE html>
    <html><head><title>Testing</title></head>
    <body>
        <p>Hello from simple route</p>
        <p>'.$phpinfo.'</p>
    </body></html>'; 
});

Route::get('/info2', function() { return view('info'); });

Why i am using ob_start()/ob_get_clean()?
Look here https://www.php.net/manual/de/function.phpinfo.php

phpinfo() returns a bool, not the content itself.
The content will be outputed directly. So if you want to catch it and write it into a variable you can use ob_start()/ob_get_clean() to do that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading