Php – Should PHP view files be called something other than ‘.php’

htmlmvcPHPpresentationview

By default, any file that PHP touches is usually suffixed with .php. It's universally understood by Apache / Nginx as the default for PHP files and most setups expect PHP files to end in this extension. In short, .php is the standard for everything PHP.

However, I'm wondering if perhaps view files should have a different extension to help differentiate them from other PHP files.

First, when it comes to views I have found that almost all MVC frameworks using a matching view file named after the controller or method. In addition, you generally also have a matching model named the same thing. This causes a problem with most IDE's and editors.

For example, you might have a "user" controller, a "user" view, and a "user" model. The results in having three files open called "user.php" which makes it a bother when you are moving around and clicking on the wrong tabs.

Second, separating views as a fundamentally different kind of PHP file (the presentation type) is another argument for changing the extension of the view files to something other than .php. Something that immediately tells your brain what type of content belongs in it.

Third, some applications expose parts (or all) of the PHP files in the webroot and it's directories. Rather than adding something like <?php if(!defined(ABC)) die('not allowed'); it would be nice to have an easy way to forbid access to views while allowing other files like the customary index.php.

Are there any other good/bad reasons to think it's a good idea to change the file extension of view files?

Best Answer

When you're talking about convention, there are a few bajillion different ways to do it. Ways I've seen:

index.tpl.php
index.phtml
templates/index.php
views/index.php

You can combine these methods to get some really amazing conventions!

views/templates/index.tpl.phtml

But if you ever do that you should feel bad about yourself.

The long and short of it - if you're using a framework, use their template convention. If you're doing it your own way, just be consistent, and, most importantly, if your template files are in web root and not named *.php make sure your webserver does not serve them as text!!!!!