Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Dave Faliskie
Dave Faliskie
17,781 Points

Subfolders with PHP

should every page on a site be in its own subfolder and renamed to index.php? what would the pros/ cons of mapping a site this way be?

1 Answer

No, definitely not. When you start building larger applications that need to scale you'll find that mapping urls/pages directly to files won't serve you well. This is due to the large amount of code you'll be having to repeat in each of your index.php files. You should look at implementing a front controller pattern: http://www.sitepoint.com/front-controller-pattern-1/

In a nutshell, you're basically routing every request to your site/app through the index.php file. Once the request reaches that file, you can examine the url to determine which files you need to include (via include, require_once, etc) to execute the given request. This pattern is used by every major php framework today (Laravel, Symfony, Cakephp, etc) and is very powerful. As an example of the benefits this approach provides, consider a full blown site with a rich layout (header, footer, sidebar, etc). If you implement a front controller pattern you can break your template into a separate file (or files) and include those inside the index.php file for every request. This would allow you to make changes to your template and it take effect for every page in your site. If you don't use this pattern you're pretty much going to be repeating the same HTML across every .php file in your app. This violates the DRY (don't repeat yourself) principle. Making even the smallest change to your template (e.g changing the copyright year) would have to be repeated for every file in your app. If your app is large you might find yourself changing hundreds of files. Hope this helps!

Dave Faliskie
Dave Faliskie
17,781 Points

Thanks that helps a lot. I felt like naming every file index.php would get confusing and didn't understand why we would do that. It makes more sense that we wouldn't. I'll def look into a front controller pattern and learn how to use it.

Dave Faliskie
Dave Faliskie
17,781 Points

Thanks that helps a lot. I felt like naming every file index.php would get confusing and didn't understand why we would do that. It makes more sense that we wouldn't. I'll def look into a front controller pattern and learn how to use it.