Follow me to learn Laravel's configuration laravel_php instance

Source: Internet
Author: User
Tags closure

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

Gets the value of a configuration item * *

Copy Code code as follows:

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

If the configuration entry does not exist, you can also specify a default value to return:

Copy Code code as follows:

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

Assigning values to a configuration item

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

Copy Code code as follows:

Config::set (' Database.default ', ' SQLite ');

The configuration values that are set when the program is run are only valid in this request and do not affect future requests.

Environment configuration

It is often useful for applications to determine the values of different configuration items based on different operating environments. For example, you might want to use a different caching driver (cache driver) on the development machine and the production machine. This can be easily achieved by changing the configuration according to the environment.

In the config directory, create a directory with the same name as your environment name, such as Local. Then, create a configuration file that contains the configuration options that you want to overwrite. For example, to overwrite cache driver in a local environment, you can create a cache.php file in the App/config/local directory and include the following:

Copy Code code as follows:

<?php

Return Array (

' Driver ' => ' file ',

);

Note: Do not use ' testing ' as the name of the environment, which is reserved specifically for unit tests.
Note that you do not need to specify a value for all configuration items in the underlying configuration file, just specify the configuration options you need to overwrite. The environment profile will overwrite the base configuration file in "Cascade" mode.

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

Copy Code code as follows:

<?php

$env = $app->detectenvironment (Array (

' Local ' => array (' Your-machine-name '),

));

In this case, ' local ' is the name of the running environment, ' 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 you are using.

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

Copy Code code as follows:

$env = $app->detectenvironment (function ()
{
Return $_server[' my_laravel_env '];
});

Get the current application environment

You can call the environment method universally to get the current application environment:

Copy Code code as follows:

$environment = App::environment ();

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

Copy Code code as follows:

if (app::environment (' local '))
{
The environment is local
}

if (app::environment (' local ', ' staging '))
{
The environment is either a local OR staging ...
}

Maintenance Mode

When the application is in maintenance mode, all routes point to a custom view. This is convenient for temporary "disable" current applications when updating applications or performing maintenance tasks. APP: The:d own method is defined in the app/start/global.php file, and it will display the output of the method to the user when the mode is maintained.

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

Copy Code code as follows:

PHP Artisan Down

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

Copy Code code as follows:

PHP Artisan up

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

Copy Code code as follows:

APP::d Own (function ()
{
return Response::view (' Maintenance ', array (), 503);
});

If the closed packet return value passed to the down method is NULL, the maintenance mode is ignored in this request.

Maintenance Mode & Queues

When the application is in maintenance mode, the new Queue task is not accepted. Once the application exits maintenance mode, the processing of the queue task returns to normal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.