CakePHP-项目配置



在本章中,我们将了解CakePHP中的环境变量,常规配置,数据库配置电子邮件配置

配置CakePHP默认情况下附带一个配置文件,我们可以根据需要对其进行修改。为此有一个专用文件夹“ config” 。 CakePHP带有不同的配置选项。

让我们首先了解CakePHP中的环境变量。

环境变量

环境变量使您的应用程序在不同环境下的工作变得容易。例如,在开发服务器,测试服务器,登台服务器和生产服务器环境上。对于所有这些环境,您都可以使用env()函数来读取所需环境的配置并构建应用程序。

在您的config文件夹中,您将看到config / .env.example。该文件包含将根据您的环境更改的所有变量。首先,您可以在config文件夹中创建一个文件,即config / .env并定义这些变量并使用它们。如果需要任何其他变量,可以将其放入该文件中。

您可以使用env()函数读取环境变量,如下所示-

$debug = env('APP_DEBUG', false);

第一个是所需环境变量的名称,第二个值是默认值。如果找不到环境变量的值,则使用默认值。

常规配置

下表描述了各种变量的作用以及它们如何影响CakePHP应用程序。

Sr.No Variable Name & Description
1

debug

Changes CakePHP debugging output.

false = Production mode. No error messages, errors, or warnings shown.

true = Errors and warnings shown.

2

App.namespace

The namespace to find app classes under.

3

App.baseUrl

Un-comment this definition, if you don’t plan to use Apache’s mod_rewrite with CakePHP. Don’t forget to remove your .htaccess files too.

4

App.base

The base directory the app resides in. If false, this will be auto detected.

5

App.encoding

Define what encoding your application uses. This encoding is used to generate the charset in the layout, and encode entities. It should match the encoding values specified for your database.

6

App.webroot

The webroot directory.

7

App.wwwRoot

The file path to webroot.

8

App.fullBaseUrl

The fully qualified domain name (including protocol) to your application’s root.

9

App.imageBaseUrl

Web path to the public images directory under webroot.

10

App.cssBaseUrl

Web path to the public css directory under webroot.

11

App.jsBaseUrl

Web path to the public js directory under webroot.

12

App.paths

Configure paths for non-class based resources. Supports the plugins, templates, locales, subkeys, which allow the definition of paths for plugins, view templates and locale files respectively.

13

Security.salt

A random string used in hashing. This value is also used as the HMAC salt when doing symmetric encryption.

14

Asset.timestamp

Appends a timestamp, which is last modified time of the particular file at the end of asset files URLs (CSS, JavaScript, Image) when using proper helpers. The valid values are −

  • (bool) false – Doesn’t do anything (default).

  • (bool) true – Appends the timestamp, when debug is true.

  • (string) ‘force’ – Always appends the timestamp.

数据库配置

可以在config / app.php和config / app_local.php文件中配置数据库。该文件包含具有提供的参数的默认连接,可以根据我们的选择进行修改。

以下代码段显示了默认参数和值,应根据要求进行修改。

Config / app_local.php

*/
   'Datasources' => [
      'default' => [
         'host' => 'localhost',
         'username' => 'my_app',
         'password' => 'secret',
         'database' => 'my_app',
         'url' => env('DATABASE_URL', null),
      ],
      /*
         * The test connection is used during the test suite.
      */
      'test' => [
         'host' => 'localhost',
         //'port' => 'non_standard_port_number',
         'username' => 'my_app',
         'password' => 'secret',
         'database' => 'test_myapp',
         //'schema' => 'myapp',
      ],
   ],

让我们在config / app_local.php中详细了解每个参数。

Host

The database server’s hostname (or IP address).

username

Database username

password

Database password.

database

Name of Database.

Port

The TCP port or Unix socket used to connect to the server.

config / app.php

'Datasources' => [
   'default' => [
      'className' => Connection::class,
      'driver' => Mysql::class,
      'persistent' => false,
      'timezone' => 'UTC',
      //'encoding' => 'utf8mb4',
      'flags' => [],
      'cacheMetadata' => true,
      'log' => false,
      'quoteIdentifiers' => false,
      //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
   ],
]   

让我们在config / app.php中详细了解每个参数。

log

Sr.No Key & Description
1

className

The fully namespaced class name of the class that represents the connection to a database server. This class is responsible for loading the database driver, providing SQL transaction mechanisms and preparing SQL statements among other things.

2

driver

The class name of the driver used to implement all specificities for a database engine. This can either be a short classname using plugin syntax, a fully namespaced name, or a constructed driver instance. Examples of short classnames are Mysql, Sqlite, Postgres, and Sqlserver.

3

persistent

Whether or not to use a persistent connection to the database.

4

encoding

Indicates the character set to use, when sending SQL statements to the server like ‘utf8’ etc.

5

timezone

Server timezone to set.

6

init

A list of queries that should be sent to the database server as and when the connection is created.

7

log

Set to true to enable query logging. When enabled queries will be logged at a debug level with the queriesLog scope.

8

quoteIdentifiers

Set to true, if you are using reserved words or special characters in your table or column names. Enabling this setting will result in queries built using the Query Builder having identifiers quoted when creating SQL. It decreases performance.

9

flags

An associative array of PDO constants that should be passed to the underlying PDO instance.

10

cacheMetadata

Either boolean true, or a string containing the cache configuration to store meta data in. Having metadata caching disable is not advised and can result in very poor performance.

电子邮件设定

可以在文件config / app.php中配置电子邮件。不需要在config / app.php中定义电子邮件配置。没有电子邮件就可以使用。只需使用相应的方法分别设置所有配置或加载配置数组即可。使用config()configTransport()创建电子邮件默认设置。

电子邮件配置传输

通过与传输配置文件分开定义传输,您可以轻松地在多个配置文件中重复使用传输配置。您可以为生产,开发和测试指定多种配置。每个传输都需要一个className。有效选项如下-

  • 邮件-使用PHP邮件函数发送

  • Smtp-使用SMTP发送

  • 调试-不发送电子邮件,仅返回结果

您可以通过将适当的文件添加到src / Mailer / Transport来添加自定义传输(或覆盖现有传输)。运输工具应命名为YourTransport.php ,其中“ Your”是运输工具的名称。

以下是电子邮件配置传输的示例。

'EmailTransport' => [
   'default' => [
      'className' => 'Mail',
      // The following keys are used in SMTP transports
      'host' => 'localhost',
      'port' => 25,
      'timeout' => 30,
      'username' => 'user',
      'password' => 'secret',
      'client' => null,
      'tls' => null,
      'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
   ],
],

电子邮件传递配置文件

传递配置文件使您可以预定义来自应用程序的电子邮件的各种属性,并为设置指定名称。这样可以节省整个应用程序的重复,并使维护和开发更加容易。每个配置文件都接受许多密钥。

以下是电子邮件传递配置文件的示例。

'Email' => [
   'default' => [
   
      'transport' => 'default',
      'from' => 'you@localhost',
   ],
],

Made with ❤️ in Chengdu. Copyright reserved 2019-2022.

蜀ICP备20006366号-1