PHP 引导基础知识
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3313950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Bootstrapping Basics
提问by BenV
I'm writing my first PHP app. Everyone talks about having a bootstrap.phpto initialize your app. This makes sense and I've put together one that I'm happy with. There's two things I don't understand, and which no one seems to mention:
我正在编写我的第一个 PHP 应用程序。每个人都在谈论bootstrap.php初始化您的应用程序。这是有道理的,我已经整理了一个我很满意的。有两件事我不明白,而且似乎没有人提到:
Where do I call my boostrap from? Do I include it in every page? Please tell me there's a better way...
How do I make it so my bootstrap is not called more often than needed? I assume it only needs to be called either at the start of the app or the start of a new session.
我从哪里调用我的助推器?我是否将其包含在每一页中?请告诉我有更好的方法...
我如何做到这样我的引导程序不会比需要的更频繁?我认为它只需要在应用程序开始或新会话开始时调用。
采纳答案by Jim W.
1: Generally the bootstrap is in the "application" directory. It is called from the "public" directory which is in the same level as application (not inside of it). The index.php inside the public folder should include the bootstrap.php and that is where your Document_Root should be set to (note you may need to change / add some include paths for it to work)
1:一般引导程序在“应用程序”目录中。它是从与应用程序处于同一级别(不在其内部)的“公共”目录中调用的。公共文件夹中的 index.php 应该包含 bootstrap.php,这就是您的 Document_Root 应该设置的位置(注意您可能需要更改/添加一些包含路径才能使其工作)
2: It should only be included once via the index.php file in the public folder. Just including it there should be enough, if it was done correctly.
2:它应该只通过公共文件夹中的 index.php 文件包含一次。如果正确完成,只需包含它就足够了。
Hope that helps.
希望有帮助。
回答by timdev
It depends on your application architecture.
这取决于您的应用程序架构。
If your architecture is the good old "flock of php scripts" - PHP scripts called directly from the browser - then you'll be including it at the top of each script, one way or another.
如果您的体系结构是古老的“php 脚本群”——直接从浏览器调用的 PHP 脚本——那么您将以一种或另一种方式将它包含在每个脚本的顶部。
Most developers (and frameworks) these days marshall all their requests through /index.php one way or another, usually with some URL rewriting going on to make nice, pretty URLs for users to see.
如今,大多数开发人员(和框架)通过 /index.php 以一种或另一种方式编组他们的所有请求,通常会进行一些 URL 重写,以便为用户创建漂亮、漂亮的 URL。
In this day and age, you should probably be doing the latter, or at least thinking about it. It leads to much better organization, and even more importantly, allows you to keep all your code outside of the web server's document root, which is a good security practice for several reasons that are outside the scope of this answer.
在这个时代,您可能应该做后者,或者至少应该考虑一下。它可以带来更好的组织,更重要的是,允许您将所有代码保存在 Web 服务器的文档根目录之外,这是一个很好的安全实践,原因超出了本答案的范围。
回答by aib
Have a look at the singleton pattern. You can double your bootstrap class as a resource container, e.g.:
看看单例模式。您可以将引导类加倍作为资源容器,例如:
$bootstrap = Bootstrap::getInstance();
$dbConn = $bootstrap->getPdoDbh();
You can includeor requirethe file, or use the autoloader and make sure you have a call to instantiate the object on all your pages. You might even have a call to getInstance()on the bottom of the file, after the class definition.
您可以include或require文件,或使用自动加载器,并确保您有一个调用来实例化所有页面上的对象。getInstance()在类定义之后,您甚至可能在文件底部调用。
Or you might use URL-based routing and have all your requests go through a single index.php file, like Zend Framework does. Or better yet, use Zend Framework.
或者您可以使用基于 URL 的路由,并让您的所有请求通过单个 index.php 文件,就像 Zend Framework 那样。或者更好的是,使用Zend Framework。
This answer assumes you're doing OOP w/ PHP >=5, which really is the way to go.
这个答案假设您正在使用 PHP >=5 进行 OOP,这确实是要走的路。
回答by Ben Johnson
One of the more elegant means by which to bootstrap a PHP application is to do so using Composer.
引导 PHP 应用程序的一种更优雅的方法是使用 Composer。
Almost every PHP library uses Composer nowadays, and requiring a Bootstrap.php-like file is as simple as:
现在几乎每个 PHP 库都使用 Composer,并且需要一个类似Bootstrap.php文件的文件非常简单:
"autoload": {
"psr-4": {
"Acme\Rocket\": "src/"
},
"files": ["src/Bootstrap.php"]
},
Note the second property, files. (The first, psr-4, is standard PSR-4 boilerplate auto-loading, and is included only to make the example more real-world.)
注意第二个属性,files。(第一个,psr-4,是标准的 PSR-4 样板自动加载,包括在内只是为了使示例更真实。)
Including the bootstrap file in this way doesn't make the naive assumption that the PHP application is executed in a web-server context, via index.php, or similar; the application could very well be a command-line application (or both, like Laravel/Artisan). Bootstrapping via the auto-loader makes this distinction a non-issue.
以这种方式包含引导文件并不会天真地假设 PHP 应用程序是在 Web 服务器上下文中执行的,通过index.php,或类似的;该应用程序很可能是一个命令行应用程序(或两者兼而有之,如 Laravel/Artisan)。通过自动加载器引导使这种区别不成问题。
回答by Timothy
it depends on what your bootstrap file does. If it's just a file that sets some ini settings and such to create a sane execution environment and establish a database connection, then simply including it with require_once in your scripts should be enough. If it's more of a single-point of entry then you can configure your server to filter all requests to it and have it dispatch to appropriate controller scripts.
这取决于您的引导程序文件的作用。如果它只是一个设置一些 ini 设置的文件,以创建一个健全的执行环境并建立数据库连接,那么只需在脚本中包含 require_once 就足够了。如果它更像是一个单点入口,那么您可以配置您的服务器以过滤对它的所有请求并将其分派到适当的控制器脚本。

