• The perfect PHP framework is your framework

    Posted on 2008-06-08 23:43:17

    Yesterday a post popped up on sitepoint stating that PHP is a framework.

    While I don't think calling PHP a framework is correct, chances are that if you use PHP, you are likely using a framework, even if it is just something you designed according to your needs.

    The reason this post got my attention was that some months ago I was evaluating different PHP frameworks, and became a bit interested in the subject. I didn't have a range of criteria or anything at the time, I just wanted something that felt right working with and that had a decent community size.
    I did take a serious shot a cakePHP however and even built a tiny custom framework based on it, but some things just didn't feel right. Since most of the projects I take part in usually don't take more than 3 weeks, and since most of the heavy work is done at the object level, why should the rest need to be complicated?

    I eventually came to the conclusion that the best PHP framework, is the one that best adapts to your needs. Considering the amount of third party extensions in repositories like PEAR or PECL, and with resources like Smarty and many others. The freedom you get from using your own custom framework, by using whatever extensions you prefer, and coding the way you are more comfortable, might just be the best choice for both small and large projects.

    Here's the structure I currently use:

    |-- classes
    | |-- page.class.php
    | |-- post.class.php
    |-- includes
    | |-- libraries
    | | |-- smarty
    | | | |-- smarty.php
    | | |-- phpmailer
    | | | |-- phpmailer.php
    | | |-- validate.class.php
    | |-- config.inc.php
    | |-- includes.inc.php
    |-- controllers
    | | |-- page.php
    | | |-- page.detail.php
    |-- templates
    | | |-- inc.header.tpl
    | | |-- inc.footer.tpl
    | | |-- page.tpl
    | | |-- page.detail.php
    | | |-- static.contacts.tpl
    | | |-- static.hp.tpl
    |-- webroot
    | |-- images
    | |-- css
    | |-- js
    | |-- index.php
    | |-- 404.html
    |-- hander.php

    I don't think it can get easier than this, but it wouldn't be the first time I'm mistaken :)
    - Webroot is the root directory in apache.
    - index.php defines the PATH constants (TEMPLATE_PATH, CONTROLLER_PATH, etc) and redirects every request to handler.php
    - handler.php parses the url (clean urls) and calls the appropriate page. For example, if http://app.com/page is called it searches for static.page.php in the templates, and if it does not exists it checks for page.php in the controllers. In page.php I just use regular php.

    Tags: php frameworks