- CakePHP - Discussion
- CakePHP - Useful Resources
- CakePHP - Quick Guide
- CakePHP - File upload
- CakePHP - Date and Time
- CakePHP - Pagination
- CakePHP - Creating Validators
- CakePHP - Validation
- CakePHP - Security
- CakePHP - Cookie Management
- CakePHP - Session Management
- CakePHP - Internationalization
- CakePHP - Form Handling
- CakePHP - Logging
- CakePHP - Errors & Exception Handling
- CakePHP - Services
- CakePHP - Delete a Record
- CakePHP - Update a Record
- CakePHP - View a Record
- CakePHP - Working with Database
- CakePHP - View Events
- CakePHP - View Elements
- CakePHP - Extending Views
- CakePHP - Views
- CakePHP - Controllers
- CakePHP - Routing
- CakePHP - Project Configuration
- CakePHP - Folder Structure
- CakePHP - Installation
- CakePHP - Overview
- CakePHP - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CakePHP - Project Configuration
In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.
Configuration CakePHP comes with one configuration file by default, and we can modify it according to our needs. There is one dedicated folder “config” for this purpose. CakePHP comes with different configuration options.
Let us start by understanding the Environment Variables in CakePHP.
Environment Variables
Environment variables make the working of your apppcation on different environments easy. For example, on dev server, test server, staging server and production server environment. For all these environments, you can make use of env() function to read the configuration for the environment you need and build your apppcation.
In your config folder, you will come across config/.env.example. This file has all the variables that will be changed based on your environment. To start with, you can create a file in config folder i.e. config/.env and define those variables and use them. In case you need any additional variables, it can go in that file.
You can read your environment variable using env() function as shown below −
Example
$debug = env( APP_DEBUG , false);
The first one is the name of the environment variable you want and second value is the default value. The default value is used, if there is no value found for the environment variable.
General Configuration
The following table describes the role of various variables and how they affect your CakePHP apppcation.
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 apppcation 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 quapfied domain name (including protocol) to your apppcation’s root. |
9 | App.imageBaseUrl Web path to the pubpc images directory under webroot. |
10 | App.cssBaseUrl Web path to the pubpc css directory under webroot. |
11 | App.jsBaseUrl Web path to the pubpc 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 vapd values are − (bool) false - Doesn’t do anything (default). (bool) true - Appends the timestamp, when debug is true. (string) ‘force’ - Always appends the timestamp. |
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.
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, Sqpte, 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 pke ‘utf8’ etc. |
5 | timezone Server timezone to set. |
6 | init A pst of queries that should be sent to the database server as and when the connection is created. |
7 | loglog 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. Enabpng 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 depvery 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. Vapd 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 , cpent => null, tls => null, url => env( EMAIL_TRANSPORT_DEFAULT_URL , null), ], ],
Email Depvery Profiles
Depvery profiles allow you to predefine various properties about email messages from your apppcation, and give the settings a name. This saves duppcation across your apppcation and makes maintenance and development easier. Each profile accepts a number of keys.
Following is an example of Email depvery profiles.
Email => [ default => [ transport => default , from => you@localhost , ], ],Advertisements