Templates & Views/Controllers: Where do you keep them?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Templates & Views/Controllers: Where do you keep them?

Post by allspiritseve »

I've heard mixed opinions about whether templates should go above or below the document root. I'm keeping all my other PHP scripts below the doc root, so it sort of makes sense to me to keep them below.

If I think of templates, views, and controllers in terms of being part of the presentation, I'm tempted to place them with my other application-specific code. However, when I think of them in terms of integral parts of my applications (forms, ajax, etc.) I'm tempted to place them in my shared library of code. This makes sense to me because I'm creating a lot of shared components that many different sites will use. However, I'd like to be able to override templates, views, and/or controllers on a case-by-case basis. Up until now I've basically been throwing all my templates in a folder, but now I'm wondering if it might be better to [pseudo] namespace them like I do the rest of my code (Zend/PEAR conventions).

Any thoughts? Where do you guys keep all of your templates, views and controllers if they are to be used by many different applications? How do you deal with overriding them? (Especially templates).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Templates & Views/Controllers: Where do you keep them?

Post by Christopher »

I like modules, so I tend to organize things into site wide, module specific or controller specific. As far as below or above document root, I don't think it should matter. The 'application' directory should be relocatable based on configuration. So it could be in a central spot for multiple sites to share, or above document root, or in the document directory to conveniently keep everything together.
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Templates & Views/Controllers: Where do you keep them?

Post by kaisellgren »

Logically, it makes sense to put all "viewable" data inside the document root and leave the rest outside. However, many clients would not be able to crawl outside their document root with an FTP program, for instance. So, that could make the process of installing new themes, plugins, whatsoever harder for the client. For that reason, I always have themes and such files inside the document root. And what comes to security, any data related to security should be put outside of the document root. For instance, having an admin control panel files outside is a better approach than having them inside the document root in terms of security.

If you have a config or a boot file, it must be within your application folder... because otherwise you (the script) wouldn't know where to look for that file and if you specify a specific location, then multiple installations of your application on the same hosting account is futile.

You could, however, implement a system where the client puts the themes on a public location, which then your application notices and actually moves the theme files outside. Or, you let them download the themes through your application. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Templates & Views/Controllers: Where do you keep them?

Post by Christopher »

kaisellgren wrote:Logically, it makes sense to put all "viewable" data inside the document root and leave the rest outside. However, many clients would not be able to crawl outside their document root with an FTP program, for instance.
I am not sure why it makes any more "sense" to put "viewable" data inside document root than anywhere else. Obviously directly accessed HTML, PHP, etc. files need to be in document root for the web server to access them. But I usually only have index.php, images, Javascript and CSS files in document root. There is no technical reason for files to be in document root other than the web server needing them to be there. Certainly, for example, CMSs keep "viewable" data in a database which is not in document root.
kaisellgren wrote:So, that could make the process of installing new themes, plugins, whatsoever harder for the client. For that reason, I always have themes and such files inside the document root. And what comes to security, any data related to security should be put outside of the document root. For instance, having an admin control panel files outside is a better approach than having them inside the document root in terms of security.
I think most hosting plans give you, at least, access to the directory above document root so you can easily place files outside of document root.
kaisellgren wrote:If you have a config or a boot file, it must be within your application folder... because otherwise you (the script) wouldn't know where to look for that file and if you specify a specific location, then multiple installations of your application on the same hosting account is futile.
I am not exactly clear the difference between your application folder and document root folder. If you have multiple domains using the same application code then they usually each need their own configuration data. You can keep it all together in a central location. I find it easier to keep it with each individual domain.
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Templates & Views/Controllers: Where do you keep them?

Post by kaisellgren »

arborint wrote:I am not sure why it makes any more "sense" to put "viewable" data inside document root than anywhere else.
If you have a file "styles.css", the client will request it and therefore it have has to be put inside the document root. If you have a file "index.php", the situation differs. By "viewable", I was referring to the situation whether the file has to be accessed through the URL by the client. For instance, the client never has to access a file "mysql.class.db", and it should be put outside the document root. That is what I meant.
arborint wrote:I think most hosting plans give you, at least, access to the directory above document root so you can easily place files outside of document root.
I know most do, but certainly not all ~ 1-10% to my experience.
arborint wrote:I am not exactly clear the difference between your application folder and document root folder. If you have multiple domains using the same application code then they usually each need their own configuration data. You can keep it all together in a central location. I find it easier to keep it with each individual domain.
Hehe, my clear terms. :)

Application folder = the folder under where your script "main" app lies. This is not always the root folder. E.g. http://site.com/forums/ and you would have phpBB application installed under /forums/ which is the application folder. So, if you are writing an application that others are going to use. For instance, I might like phpBB so much that I register 100 domains (for 100 forums) under the same dedicated server. If they all make a config file outside the document root in a specific location, they might collide depending on the setup of your domains. So, that's why I said that config file have to be in the application folder - so that it will never collide with other config files.

The other files, on the other hand, can be located outside the document root, because the config file will tell PHP where to look for so you can setup a unique folder outside the document root (like "phpbb_sdf3f43te45yh").
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Templates & Views/Controllers: Where do you keep them?

Post by allspiritseve »

arborint wrote:I like modules, so I tend to organize things into site wide, module specific or controller specific.
Ok, so you would favor a directory structure like this:

Code: Select all

/Icebox
    /Blog
        /Controllers
            ListController.tpl
            SingleController.tpl    
        /Templates/
            List.tpl
            Single.tpl
I like that, it looks really clean including 'Icebox/Blog/Templates/List.tpl'. The thing is, /Icebox is all shared code that many different sites will be using (I'm using svn:externals to manage that, same thing I did with SimpleTest for skeleton). I'd like to isolate changes as much as possible, and templates are going to change more than anything between sites.

I wonder if some sort of DI container for templates would be useful... so I could call:

Code: Select all

$template =  $container->include ('Blog/List');
And have the capability of specifying which template to use ('Icebox/Blog/Templates/List.tpl' or 'App/Blog/Templates/List.tpl'). Does that sound feasible?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Templates & Views/Controllers: Where do you keep them?

Post by josh »

All I have in my document root is index.php for Ne8. One of my requirements is pass all access to CSS and other resources thru a permissions system. If user A is using template A it doesnt make sense to bomb his SEO by letting google index 1000s of other files from other templates, and things like that.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Templates & Views/Controllers: Where do you keep them?

Post by allspiritseve »

josh wrote:All I have in my document root is index.php for Ne8. One of my requirements is pass all access to CSS and other resources thru a permissions system. If user A is using template A it doesnt make sense to bomb his SEO by letting google index 1000s of other files from other templates, and things like that.
Yeah, I think I'm moving in that direction.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Templates & Views/Controllers: Where do you keep them?

Post by Christopher »

allspiritseve wrote:I like that, it looks really clean including 'Icebox/Blog/Templates/List.tpl'. The thing is, /Icebox is all shared code that many different sites will be using (I'm using svn:externals to manage that, same thing I did with SimpleTest for skeleton). I'd like to isolate changes as much as possible, and templates are going to change more than anything between sites.
I think the main way to solve that is with conventions so that there are clear contracts between the module/controller levels and global level. That way it is explicit how each site customizes things.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Templates & Views/Controllers: Where do you keep them?

Post by josh »

allspiritseve wrote:Yeah, I think I'm moving in that direction.
The other great thing about it is I have the "SkinController" check a list of skin locations, it uses the first hit, that way I can have it serve out of userA/ but if the requested file is not present for that template it would goto the default/ folder next, allowing me to centralize common elements like CSS resets, jquery scripts, generic icons, application-wide resources, etc... that way if I wanted 2 templates and there was only minor changes, I can just make template A 'extend' template B if you wanna call it that..
Post Reply