All configuration files about the Laravel framework are stored in the appconfig directory. Configuration options in all files are described in this document, so you can easily view these files and familiarize yourself with these configuration items. You can use the Config class to access configuration items during runtime:
Obtain the value of a configuration item **
The code is as follows:
Config: get ('app. timezone ');
If the configuration item does not exist, you can specify the default value returned:
The code is as follows:
$ Timezone = Config: get ('app. timezone ', 'utc ');
Assign values to configuration items
Note: the "point" syntax can be used to access the values of configuration items in different files. You can also assign values to configuration items at runtime. :
The code is as follows:
Config: set ('database. default', 'sqlite ');
The configuration value set when the program is running is only valid in this request and will not affect future requests.
Environment configuration
Generally, it is very useful for an application to determine the values of different configuration items based on different runtime environments. For example, you may want to use a different cache driver on the development machine and production machine ). You can easily change the configuration according to the environment.
Create a directory with the same name as your environment in the config Directory, for example, local. Create a configuration file that contains the configuration options you want to override. For example, to overwrite the cache driver in a local environment, you can create a cache. php file in the app/config/local directory and include the following content:
The code is as follows:
<? Php
Return array (
'Driver '=> 'File ',
);
Note: Do not use 'testing' as the environment name, which is reserved for unit testing.
Note: You do not need to specify values for all configuration items in the basic configuration file. you only need to specify the configuration options you need to override. The environment configuration file overwrites the basic configuration file in cascade mode.
Next, we need to guide the framework to determine its runtime environment. The default environment is always produciton. However, you can set other environments in the bootstrap/start. php file under the root directory of the installation directory. In this file, you can find $ app-> detectEnvironment method call. The input array parameter is used to determine the current runtime environment. You can add other environments or machine names as needed.
The code is as follows:
<? Php
$ Env = $ app-> detectEnvironment (array (
'Local' => array ('your-machine-name '),
));
In this case, 'local' is the name of the running environment, and 'Your-machine-name' is the host name of the server. On Linux and Mac, you can use the hostname command to determine the host name of the machine in use.
If you need a more flexible environment check method, you can pass a Closure (Closure) when calling detectEnvironment, so that you can check the environment in your own way:
The code is as follows:
$ Env = $ app-> detectEnvironment (function ()
{
Return $ _ SERVER ['My _ LARAVEL_ENV '];
});
Obtain the current application environment
You can call the environment method to obtain the current application environment:
The code is as follows:
$ Environment = App: environment ();
You can also pass the parameter to the environment method to determine whether the application environment matches a given value:
The code is as follows:
If (App: environment ('Local '))
{
// The environment is local
}
If (App: environment ('local', 'staging '))
{
// The environment is either local OR staging...
}
Maintenance Mode
When the application is in maintenance mode, all routes point to a custom view. This is convenient to temporarily "disable" The current application when updating the application or performing maintenance tasks. App: The down method is defined in the app/start/global. php file. it displays the content output by this method in maintenance mode.
To enable the maintenance mode, run the down command of Artisan:
The code is as follows:
Php artisan down
To disable the maintenance mode, run the up command:
The code is as follows:
Php artisan up
When your application is in maintenance mode, to display a custom View, add the following code to the app/start/global. php file:
The code is as follows:
App: down (function ()
{
Return Response: view ('maintenance ', array (), 503 );
});
If the return value of the closure passed to the down method is NULL, the maintenance mode will be ignored in this request.
Maintenance Mode & queue
When the application is in maintenance mode, new queue tasks are not accepted. Once the application exits the maintenance mode, the processing of the queue task returns to normal.