I work for a company (PHP shop) where we are looking at cleaning/separating our code as we move toward PSR-0 (http://www.php-fig.org/psr/psr-0/).<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/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)
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.<p>/core/apps/[name of app]<p>/core/config/ (config files, PDF templates, etc.)<p>/core/inc/ [libraries/extensions]<p>/public_html/index.php<p>/public_html/info/ [icons, images, and other public accessible data]<p>mainly index.php has<p>$home = $_SERVER['SCRIPT_FILENAME'];<p>$loc = explode('/',$home);<p>define('CORE_BASE', implode('/',array_slice($loc,0,-2)).'/');<p>require_once core_BASE.'core/inc/main.php';<p>which defines the core path (below web root), then includes the main base code. From there all code is below web root.<p>note: css and javascript can be included directly (saves having browser request them anyway)
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
Just a tip, load as much as you can get away with (configuration, ESPECIALLY) outside of your webroot.<p>BAD:<p>* /var/www/config/mysql.json<p>* /var/www/static/site.css<p>* /var/www/index.php<p>* $_SERVER['DOCUMENT_ROOT'] = '/var/www';<p>GOOD:<p>* /var/www/config/mysql.json<p>* /var/www/public/static/site.css<p>* /var/www/public/index.php<p>* $_SERVER['DOCUMENT_ROOT'] = '/var/www/public';
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.<p>[0]<a href="https://getcomposer.org" rel="nofollow">https://getcomposer.org</a>
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 <a href="http://bit.ly/1FQjpby" rel="nofollow">http://bit.ly/1FQjpby</a>