Laravel Basic Tutorial--Configuration

Source: Internet
Author: User

Laravel Basic Tutorial--Configuration

All configuration files are stored in the Config directory, and the configuration items in each configuration file are marked with a document.

Accessing configuration values

The Config global helper method is provided in the vendor/laravel/framework/src/illuminate/foundation/helpers.php file, which allows the function to be used. syntax to get the configuration item value within the file.

$value = config (' app.timezone ');

You can also pass the second parameter as the default value in the Config function and return the default value when the configuration item is not found.

$value = config (' app.timezone ', ' Asia/shanghai ');

To set the value of a configuration item:

Config ([' app.timezone ' = ' Asia/shanghai ']);

Environment configuration

We often expect the development environment and production environment to have different configurations. For example, you use different cache drivers in your on-premises development environment, and Laravel is easy to configure based on your environment.

Laravel uses the Dotenv class library to build an environment-based configuration, The default environment-based configuration information is in the. env file in the root directory, and if you are installing Laravel by composer, it will automatically copy the. env.example file and rename it to. env, if not you need to do it manually. If you add an environment-based configuration item, you might want to add the same configuration item to the. Env.example so that others can understand your configuration information based on the. Env.example in a multiplayer collaboration.

Whenever a request is received by the program, the application automatically mounts the. env file and encapsulates the configuration information in the global variable $_env, and of course you can get the environment configuration item information by using the Global helper Function env () and set it in your other configuration file, in fact, Laravel has been configured in some configuration files using this method.

' Debug ' = env (' App_debug ', false),

The second parameter in the ENV function is the default value for the configuration item, and the default value is automatically used when the configuration item is not available in the environment configuration file. env.

In addition your. Env configuration environment should not be committed to the version controller because other server environments or developer environments may need to introduce different environment configurations. For example, the production environment should not open debug, different developers of the local database configuration information may be different.

If you are developing in a team, you should introduce your additional environment configuration information into the. env.example file and submit it to other developers so they can understand what configuration information should be introduced using the part you are developing.

Determining the current environment

The current environment is defined in the APP_ENV variable in the. env file, which you can get through the environment function of the app facade :

$environment = App::environment ();

Of course you can also get through the global approach to Env or the app:

$environment = env (' app_env '); # or$environment = APP ()->environment ();

Sometimes we need to specifically identify which environment the current environment is, and to execute different business logic depending on the environment, then we need to use the environment function to determine the match, of course you can pass one or more environment parameters, as long as any match to any of them will return true :

if (app::environment (' local ')) {  //If env (' app_env ') = = = ' Local '}if (app::environment (' local ', ' staging ')) {  / /env (' app_env ') = = = ' Local ' | | Env (' app_env ') = = = ' Staging '}

Cache configuration Information

In the Config directory there are many configuration files, configuration files have different configuration information, in order to start the program more quickly, we can in the development environment to set these configuration information in a configuration file, so that the program is accessed, not every time to load n files, we can through the artisan Config:cache command to do this thing. All profiles are integrated into one file and loaded automatically by the program.

Of course, this is not recommended in the development environment, because the development environment we may change configuration information frequently, in order to make the configuration information effective in time we have to run the PHP artisan config:cache command Frequently, occasionally we forget to execute the command. The production environment cache profile should be normal, and the cache configuration file command should be regenerated when the version is released to regenerate the cache configuration information. You should do it as part of an automatic release.

Maintenance Mode

Laravel provides maintenance mode, and when the maintenance mode is on, all access requests are returned to a view that can be customized. If maintenance mode is turned on, each request will return a 503 status code. How to turn on Maintenance mode:

PHP Artisan Down

How to turn off Maintenance mode:

PHP Artisan up

Maintenance Mode Response Template

The view template for Maintenance mode response is stored in resources/views/errors/503.blade.php and you are free to modify it.

Maintenance Mode & Queues

When maintenance mode is on, the queue is paused for execution, and the queue resumes processing when maintenance mode is turned off.

Alternative to Maintenance mode

Since opening maintenance mode requires shutting down the application for a while, you might consider a continuous integration service like Envoyer that does not need to shut down the application.

  • 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.