TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: PHP Directory structure and code organization

3 点作者 joell超过 10 年前
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)

6 条评论

LarryMade2超过 10 年前
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>&#x2F;core&#x2F;apps&#x2F;[name of app]<p>&#x2F;core&#x2F;config&#x2F; (config files, PDF templates, etc.)<p>&#x2F;core&#x2F;inc&#x2F; [libraries&#x2F;extensions]<p>&#x2F;public_html&#x2F;index.php<p>&#x2F;public_html&#x2F;info&#x2F; [icons, images, and other public accessible data]<p>mainly index.php has<p>$home = $_SERVER[&#x27;SCRIPT_FILENAME&#x27;];<p>$loc = explode(&#x27;&#x2F;&#x27;,$home);<p>define(&#x27;CORE_BASE&#x27;, implode(&#x27;&#x2F;&#x27;,array_slice($loc,0,-2)).&#x27;&#x2F;&#x27;);<p>require_once core_BASE.&#x27;core&#x2F;inc&#x2F;main.php&#x27;;<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)
twunde超过 10 年前
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&#x27;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&#x27;s code 3. Start putting common utility code into helpers, and base controllers&#x2F;models. 4. If you have common view components, consider breaking them out into partials
sarciszewski超过 10 年前
Just a tip, load as much as you can get away with (configuration, ESPECIALLY) outside of your webroot.<p>BAD:<p>* &#x2F;var&#x2F;www&#x2F;config&#x2F;mysql.json<p>* &#x2F;var&#x2F;www&#x2F;static&#x2F;site.css<p>* &#x2F;var&#x2F;www&#x2F;index.php<p>* $_SERVER[&#x27;DOCUMENT_ROOT&#x27;] = &#x27;&#x2F;var&#x2F;www&#x27;;<p>GOOD:<p>* &#x2F;var&#x2F;www&#x2F;config&#x2F;mysql.json<p>* &#x2F;var&#x2F;www&#x2F;public&#x2F;static&#x2F;site.css<p>* &#x2F;var&#x2F;www&#x2F;public&#x2F;index.php<p>* $_SERVER[&#x27;DOCUMENT_ROOT&#x27;] = &#x27;&#x2F;var&#x2F;www&#x2F;public&#x27;;
krapp超过 10 年前
Organizing around Composer[0] may be what you want. It&#x27;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:&#x2F;&#x2F;getcomposer.org</a>
nicks451超过 10 年前
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:&#x2F;&#x2F;bit.ly&#x2F;1FQjpby</a>
bwh2超过 10 年前
How much of a lift would it be to transition away from CodeIgniter to something like Symfony or Laravel?