
Ask HN: PHP Directory structure and code organization - joell
I work for a company (PHP shop) where we are looking at cleaning&#x2F;separating our code as we move toward PSR-0 (http:&#x2F;&#x2F;www.php-fig.org&#x2F;psr&#x2F;psr-0&#x2F;).<p>We have lots of different `features` that we would like to split up and organize in a way that makes the most sense.<p>We want:
- Highly modular.
- Easily extend&#x2F;implement abstract code between features.
- Outside the core framework.<p>Any suggested reading material, advice, or feedback would be fantastic!<p>Thanks HN! :)<p>Edit: We use CodeIgniter (earlier version)
======
LarryMade2
I moved just about everything below web root cept icons and images, and other
browser side necesities. All directs go through index.php, nothing is
referenced by directory publically.

/core/apps/[name of app]

/core/config/ (config files, PDF templates, etc.)

/core/inc/ [libraries/extensions]

/public_html/index.php

/public_html/info/ [icons, images, and other public accessible data]

mainly index.php has

$home = $_SERVER['SCRIPT_FILENAME'];

$loc = explode('/',$home);

define('CORE_BASE', implode('/',array_slice($loc,0,-2)).'/');

require_once core_BASE.'core/inc/main.php';

which defines the core path (below web root), then includes the main base
code. From there all code is below web root.

note: css and javascript can be included directly (saves having browser
request them anyway)

------
twunde
There are several solutions that work well in conjunction: 1\. Start moving
code into code libraries. These can be reused in multiple projects, are
framework-agnostic, and can be managed via composer. 2\. Once you've reached a
certain codebase size, switch to using modules. This will help create logical
separations of code, and will limit devs stepping on each other's code 3\.
Start putting common utility code into helpers, and base controllers/models.
4\. If you have common view components, consider breaking them out into
partials

------
sarciszewski
Just a tip, load as much as you can get away with (configuration, ESPECIALLY)
outside of your webroot.

BAD:

* /var/www/config/mysql.json

* /var/www/static/site.css

* /var/www/index.php

* $_SERVER['DOCUMENT_ROOT'] = '/var/www';

GOOD:

* /var/www/config/mysql.json

* /var/www/public/static/site.css

* /var/www/public/index.php

* $_SERVER['DOCUMENT_ROOT'] = '/var/www/public';

------
krapp
Organizing around Composer[0] may be what you want. It's extremely flexible in
terms of the kinds of packages it can load, implements PSR-0 and PSR-4 and
allows custom autoloading, and binding scripts to pre and post installs.

[0][https://getcomposer.org](https://getcomposer.org)

------
nicks451
Are you already using a framework? If so which one? If not, I would check out
the book Modernizing Legacy Applications In PHP by Paul M. Jones
[http://bit.ly/1FQjpby](http://bit.ly/1FQjpby)

------
bwh2
How much of a lift would it be to transition away from CodeIgniter to
something like Symfony or Laravel?

