首页  >  文章  >  后端开发  >  CakePHP 项目配置

CakePHP 项目配置

WBOY
WBOY原创
2024-09-10 17:25:03262浏览

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

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

让我们从了解 CakePHP 中的环境变量开始。

环境变量

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

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

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

示例

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

第一个是您想要的环境变量的名称,第二个值是默认值。如果没有找到环境变量的值,则使用默认值。

常规配置

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

表>

Databases Configuration

Database can be configured in config/app.php and config/app_local.php file. This file contains a default connection with provided parameters, which can be modified as per our choice.

The below snippet shows the default parameters and values, which should be modified as per the requirement.

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',
      ],
   ],

Let us understand each parameter in detail in config/app_local.php.

先生No 变量名称和描述
1
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.

调试 更改 CakePHP 调试输出。 false = 生产模式。没有显示错误消息、错误或警告。 true = 显示错误和警告。
2 App.namespace 用于在其下查找应用程序类的命名空间。
3 App.baseUrl 如果您不打算将 Apache 的 mod_rewrite 与 CakePHP 一起使用,请取消注释此定义。不要忘记也删除您的 .htaccess 文件。
4 App.base 应用程序所在的基本目录。如果为 false,则会自动检测到。
5 App.encoding 定义您的应用程序使用的编码。此编码用于生成布局中的字符集并对实体进行编码。它应该与为您的数据库指定的编码值匹配。
6 App.webroot webroot 目录。
7 App.wwwRoot webroot 的文件路径。
8 App.fullBaseUrl 应用程序根目录的完全限定域名(包括协议)。
9 App.imageBaseUrl webroot 下公共图像目录的 Web 路径。
10 App.cssBaseUrl webroot 下公共 css 目录的 Web 路径。
11 App.jsBaseUrl webroot下公共js目录的Web路径。
12 App.paths 配置非基于类的资源的路径。支持插件、模板、语言环境、子项,它们允许分别定义插件、视图模板和语言环境文件的路径。
13 Security.salt 用于散列的随机字符串。在进行对称加密时,该值也用作 HMAC salt。
14 资产.时间戳 使用正确的帮助程序时,在资产文件 URL(CSS、JavaScript、图像)末尾附加一个时间戳,即特定文件的最后修改时间。有效值为 -
  • (bool) false - 不执行任何操作(默认)。
  • (bool) true - 当 debug 为 true 时附加时间戳。
  • (string) ‘force’ - 始终附加时间戳。
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'],
   ],
]   

Let us understand each parameter in detail in 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.

Email Configuration

Email can be configured in file config/app.php. It is not required to define email configuration in config/app.php. Email can be used without it. Just use the respective methods to set all configurations separately or load an array of configs. Configuration for Email defaults is created using config() and configTransport().

Email Configuration Transport

By defining transports separately from delivery profiles, you can easily re-use transport configuration across multiple profiles. You can specify multiple configurations for production, development and testing. Each transport needs a className. Valid options are as follows −

  • Mail − Send using PHP mail function

  • Smtp − Send using SMTP

  • Debug − Do not send the email, just return the result

You can add custom transports (or override existing transports) by adding the appropriate file to src/Mailer/Transport. Transports should be named YourTransport.php, where 'Your' is the name of the transport.

Following is the example of Email configuration transport.

'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 Delivery Profiles

Delivery profiles allow you to predefine various properties about email messages from your application, and give the settings a name. This saves duplication across your application and makes maintenance and development easier. Each profile accepts a number of keys.

Following is an example of Email delivery profiles.

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

以上是CakePHP 项目配置的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:CakePHP Folder Structure下一篇:CakePHP Routing

相关文章

查看更多