CakePHP 專案配置
本章我們將了解CakePHP中的環境變數、常規設定、資料庫設定和郵件配置。
設定 CakePHP 預設自帶一個設定文件,我們可以依照自己的需求進行修改。為此,有一個專用資料夾 “config”。 CakePHP 具有不同的配置選項。
讓我們先從了解 CakePHP 中的環境變數開始。
環境變數
環境變數使您的應用程式在不同環境下的工作變得容易。例如,在開發伺服器、測試伺服器、登台伺服器和生產伺服器環境上。對於所有這些環境,您可以使用 env() 函數 讀取所需環境的配置並建立您的應用程式。
在您的 config 資料夾中,您將看到 config/.env.example。該文件包含將根據您的環境進行更改的所有變數。首先,您可以在 config 資料夾中建立一個文件,即 config/.env 並定義這些變數並使用它們。如果您需要任何其他變量,可以將其放入該文件中。
您可以使用 env() 函數讀取環境變量,如下所示 -
範例
$debug = env('APP_DEBUG', false);
第一個是您想要的環境變數的名稱,第二個值是預設值。如果沒有找到環境變數的值,則使用預設值。
常規設定
下表描述了各種變數的作用以及它們如何影響您的 CakePHP 應用程式。
先生No | 變數名稱和描述 | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
|
||||||||||||||||||||||||||||||
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目錄的網路路徑。 | ||||||||||||||||||||||||||||||
12 | App.paths 配置非基於類別的資源的路徑。支援外掛程式、範本、語言環境、子項目,它們允許分別定義外掛程式、視圖範本和語言環境文件的路徑。 | ||||||||||||||||||||||||||||||
13 | Security.salt 用於散列的隨機字串。在進行對稱加密時,該值也用作 HMAC salt。 | ||||||||||||||||||||||||||||||
14 | 資產.時間戳
使用正確的幫助程式時,在資產文件 URL(CSS、JavaScript、圖像)末尾附加一個時間戳,即特定文件的最後修改時間。有效值為 -
|
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.
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
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。
