Page 1 of 1

Project directory structure and naming

Posted: Tue Jan 26, 2010 10:57 am
by I0printRob
Hi,
I'm from a front end background so everything i'm reading is just confusing me more I think?

I'm currently putting the front end in the root (css folder, js folder, pages that output html).
Then I have a folder (also in the root) called "php" which has files like "create_account.php", "login.php", "get_autocomplete.php" etc.
They don't output html, they just read and write to the database or session etc.
Within that folder is one called "includes" which has "db_connect.php" etc.

Is that a bad structure? I don't think that "php" is a very good name for that folder as all the front end is also php anyway?
I've read that you should put only the public files together and move the rest elsewhere?

Re: Project directory structure and naming

Posted: Tue Jan 26, 2010 2:14 pm
by pickle
There's really no right or wrong way to organize, as long as it's organized. If you've got a folder for your CSS files, make sure all your CSS files go in there - don't spread them out.

For some of my projects, I've got just one directory - includes/. Inside the includes/ directory I have classes/ and templates/ (which then holds js/). I've had other projects where the classes/ and templates/ folders are right in the root, with no special folder for Javascript (as there was only one or two).

It's like a website itself. As long as the information is organized in a rational manner, you should be able to find what you're looking for.

Re: Project directory structure and naming

Posted: Tue Jan 26, 2010 3:48 pm
by flying_circus
I like to put any php included files (that dont require direct access) in a folder outside of the wwwroot.

Re: Project directory structure and naming

Posted: Tue Jan 26, 2010 10:19 pm
by Luke
Here is how I organize my applications, but what works for me might not work for you and vice-versa.

Code: Select all

/app
    /config
        acl.php
        app.ini
        routes.php
    /modules
        /default
        /admin
        /blog
    /ext
        /Controller
            /Helper
            /Plugin
        /MyApp
    /model
        Category.php
        Post.php
        User.php
    Bootstrap.php
/lib
    /Zend
    /Swift
    /MyFramework
/log
    app.log
    errors.log
/webroot
    /css
    /js
    /img
    index.php
 
The "app" directory contains all of my controllers, configuration, models, views, and any other application-specific code and libraries.
The "app/ext" directory contains all of the application-specific library code (for instance, controller action helpers, controller plugins, etc.)
The "lib" directory contains my framework code, the Zend Framework, and any other libraries that are not application-specific.
The "log" directory contains logs.
The "webroot" directory is what apache's webroot directive is set to. The "index.php" file calls the "app/Bootstrap.php" file.

Re: Project directory structure and naming

Posted: Wed Jan 27, 2010 7:16 am
by I0printRob
Thanks a lot for the advice. I shall move what I can out of webroot and into a clearer structure.

Having moved some files, they don't run in the same way when moved out of webroot. They only work for me as includes.
Is that the way to implement this structure? I will gladly research this with some pointers if the subject is broad.

To clarify: if you point a form at the file via a relative path, there is a 404 error.

Re: Project directory structure and naming

Posted: Wed Jan 27, 2010 8:27 am
by Eran
Luke is using a pattern called Front Controller which allows him to route all requests through one script (index.php located at the /webroot folder). If you are not using this pattern or MVC in general, his directory structure might not be applicable to your case.

Re: Project directory structure and naming

Posted: Wed Jan 27, 2010 10:30 am
by I0printRob
Interesting. Thanks for clarifying.

Re: Project directory structure and naming

Posted: Thu Jan 28, 2010 3:02 am
by Luke
Thanks for clarifying, pytrin. I should have made that clear when I posted my structure. Here, I'll explain a little further. I0printRob, if you have any questions, I'll do my best to answer them.

I use virtual hosts within my apache configuration. If you are not familiar with virtual hosts, basically what they do is allow you to run multiple sites with one instance of apache (for more info about virtual hosts, check out this page). This is what my virtual host looks like:

Code: Select all

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/home/example.com/htdocs/web"
    <Directory "/home/example.com/htdocs/web">
        Options +Indexes FollowSymLinks +ExecCGI
        AllowOverride AuthConfig FileInfo
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
What this does is point "http://example.com/" to my "web" directory in the structure I posted above. My apache configuration is set to use "index.html" if the user requests a directory. If there is no "index.html", it uses "index.php" and as you will notice, there is an "index.php" in my "web" directory in the structure I posted above. So when a user requests "http://example.com/", apache serves up "web/index.php".

Here is what my "index.php" file looks like:

Code: Select all

<?php
/**
 * Define the path to the application-specific code (the "app" directory)
 */
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              realpath(dirname(__FILE__) . '/../app'));
 
/**
 * Create shortcuts to PHP's directory and path separators
 */
define('PS', PATH_SEPARATOR);
define('DS', DIRECTORY_SEPARATOR);
 
/** Zend_Application **/
require_once 'Zend/Application.php';
 
/** Set the default timezone **/
date_default_timezone_set("America/Los_Angeles");
 
/**
 * The application is "bootstrapped" here. What this means is that all of the necessary
 * library files, configuration, databases, etc. are included and initialized. 
 */
$application = new Zend_Application(
    'production',
    APPLICATION_PATH . '/config/app.ini' // path to configuration file
);
 
/**
 * And now we're off!
 */
$application->bootstrap()
            ->run();
As pytrin pointed out, I'm using the "Front Controller Pattern". This means that every page is first routed through "web/index.php". This affords me many benefits. For instance, rather than having to include my library files on every page within the application, I just include them once. Within "app/Bootstrap.php" (which, as you will remember, is included from "web/index.php"). It makes my application's URLs really clean as well since all pages are routed through the index page. For instance: "http://example.com/index.php/admin/user/create/".

Also, it allows me to build a more modular application. As you will notice, inside of the "app" directory, there is a directory called "modules". Each of these is a sort of "mini-application" within the main application. All of them can share resources, database connections, etc.

Easily the coolest benefit that comes from the front controller is something called the dispatch loop. It allows me to do all kinds of cool things. The dispatch loop is a little beyond the scope of this thread and I don't think I'd be able to properly explain it anyway. If you are interested in it, you should check out the Zend Framework Controller documentation:

http://framework.zend.com/manual/en/zen ... asics.html