Learn from me the configuration of Laravel Laravel

  • 2021-07-22 09:13:06
  • OfStack

When you need to access configuration items at run time, you can use the Config class:

Get the value of 1 CI **


Config::get('app.timezone');

If the configuration item does not exist, you can also specify the default value returned:


$timezone = Config::get('app.timezone', 'UTC');

Assigning a value to a configuration item

Note that "dot" syntax can be used to access the values of configuration items in different files. You can also assign values to configuration items at run time. :


Config::set('database.default', 'sqlite');

Configuration values set at program runtime are valid only in this request and will not affect future requests.

Environment configuration

Typically, it is useful for applications to determine different CI values depending on the environment in which they run. For example, you might want to use different cache drivers (cache driver) on development and production machines. Changing the configuration according to the environment can easily achieve this goal.

Under the config directory, create a directory with the same name as your environment name, such as local. Then, create configuration files that contain the configuration options you want to override. For example, to overwrite the cache driver in the local environment (cache driver), you can create an cache. php file in the app/config/local directory and include the following:


<?php return array(     'driver' => 'file', );

Note: Do not use 'testing' as the environment name, which is reserved specifically for unit testing.
Note that you don't need to specify values for all configuration items in the underlying configuration file, just specify the configuration options you need to override. The environment configuration file will overwrite the basic configuration file in the form of "cascade".

Next, we need to guide the framework how to determine its running environment. The default environment is always produciton. However, you can set up other environments in the bootstrap/start. php file at the root of the installation directory. In this file, you can find $app- > The call to the detectEnvironment method. The array parameters passed in are used to determine the current running environment. You can add other environment or machine names as needed.


<?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, the hostname command can be used to determine the hostname of the machine being used.

If you need more flexible environment checking methods, you can pass a closure (Closure) when calling detectEnvironment, so that you can check the environment in your own way:


$env = $app->detectEnvironment(function()
{
    return $_SERVER['MY_LARAVEL_ENV'];
});

Get the current application environment

The environment method can be commonly called to obtain the current application environment:


$environment = App::environment();

You can also pass parameters to the environment method to determine whether the application environment matches a given value:


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 will point to 1 custom view. This is convenient for temporarily "disabling" the current application while updating the application or performing maintenance tasks. The App:: down method is defined in the app/start/global. php file and will present the output of the method to the user while maintaining the mode.

To turn on maintenance mode, simply execute the down command of Artisan:


php artisan down

To turn off maintenance mode, simply execute the up command:


php artisan up

When your application is in maintenance mode, if you need to show a custom view, just add the following code to the file app/start/global. php:


$timezone = Config::get('app.timezone', 'UTC');
0
If the closure return value passed to the down method is NULL, the maintenance mode is ignored in this request.

Maintenance mode & Queue

When the application is in maintenance mode, it will not accept new queued tasks. 1 Once the application exits maintenance mode, the processing of queued tasks returns to normal.


Related articles: