应用程序

原文:http://www.yiiframework.com/doc-2.0/guide-structure-applications.html

应用程序是一个对象,这个对象掌握着整个 Yii 应用程序系统的结构和生命周期。每个 Yii 应用程序系统都包含一个应用程序对象,该对象在 入口脚本 中创建,且能够通过表达式 \Yii::$app 在全局访问。

说明:根据上下文,当我们说“一个应用程序”时,它要么代表一个应用程序对象,要么代表一个应用程序系统。

有两种应用程序对象:Web 应用程序 和 控制台应用程序。正如名所指,前者主要处理 Web 请求,而后者则处理控制台命令请求。

应用程序配置

当一个 入口脚本 创建了一个应用程序时,该应用程序将加载一个 配置 参数,然后将其应用到应用程序,如下所示:

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

// load application configuration
$config = require(__DIR__ . '/../config/web.php');

// 实例化和配置应用程序
(new yii\web\Application($config))->run();

类似于常规 配置,应用程序配置指定如何初始化应用程序对象的属性。由于应用程序配置通常非常复杂,因此配置参数通常保存在 配置文件 中,例如上述例子中的 web.php 文件。

应用程序属性

有许多重要的应用程序属性需要您在应用程序配置中指定。这些属性通常描述应用程序运行的环境。例如,应用程序需要知道如何加载 控制器,以及哪里存放临时文件等。在接下来的描述中,我们将对这些属性逐一作总结。

必要属性

在任何应用程序中,您都应当至少配置这两个属性:id 和 basePath

id

id 属性指定了一个唯一的 ID 值,用以将此应用程序与其他应用程序区分开。这个属性主要在编程中用到。出于最佳的互操作性考虑,推荐仅使用字母和数字来指定应用程序 ID 值,不过并不强求这么做。

basePath

basePath 属性指定了一个应用程序的根目录。这个目录包含了该应用程序系统所有受保护的源代码。在这个目录下,你通常会看到诸如 models、 views 和 controllers 等子目录,这些目录下包含的源代码分别对应相应的 MVC 模式。

You may configure the basePath property using a directory path or a path alias. In both forms, the corresponding directory must exist, or an exception will be thrown. The path will be normalized by calling the realpath() function.

The basePath property is often used to derive other important paths (e.g. the runtime path). For this reason, a path alias named @app is predefined to represent this path. Derived paths may then be formed using this alias (e.g. @app/runtime to refer to the runtime directory).

重要属性

The properties described in this subsection often need to be configured because they differ across different applications.

aliases

This property allows you to define a set of aliases in terms of an array. The array keys are alias names, and the array values are the corresponding path definitions. For example:

[
    'aliases' => [
        '@name1' => 'path/to/path1',
        '@name2' => 'path/to/path2',
    ],
]

This property is provided so that you can define aliases in terms of application configurations instead of by calling the Yii::setAlias() method.

bootstrap

This is a very useful property. It allows you to specify an array of components that should be run during the application bootstrapping process. For example, if you want a module to customize the URL rules, you may list its ID as an element in this property.

Each component listed in this property may be specified in one of the following formats:

  • an application component ID as specified via components,
  • a module ID as specified via modules,
  • a class name,
  • a configuration array,
  • an anonymous function that creates and returns a component.

For example:

[
    'bootstrap' => [
        // an application component ID or module ID
        'demo',

        // a class name
        'app\components\Profiler',

        // a configuration array
        [
            'class' => 'app\components\Profiler',
            'level' => 3,
        ],

        // an anonymous function
        function () {
            return new app\components\Profiler();
        }
    ],
]

Info: If a module ID is the same as an application component ID, the application component will be used during the bootstrapping process. If you want to use the module instead, you may specify it using an anonymous function like the following:

[
    function () {
        return Yii::$app->getModule('user');
    },
]

During the bootstrapping process, each component will be instantiated. If the component class implements [[yii\base\BootstrapInterface]], its bootstrap() method will also be called.

Another practical example is in the application configuration for the Basic Project Template, where the debug and gii modules are configured as bootstrapping components when the application is running in the development environment:

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

Note: Putting too many components in bootstrap will degrade the performance of your application because for each request, the same set of components need to be run. So use bootstrapping components judiciously.

TBS

最后更新于 2017-07-31 08:51 下午

关于 “应用程序” 的 1 个意见

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据