فهرست ها

Introduction

All of the configuration files for the Laravel framework are stored in the config directory. Each option is documented, so feel free to look through the files and get familiar with the options available to you.

These configuration files allow you to configure things like your database connection information, your mail server information, as well as various other core configuration values such as your application URL and encryption key.

The about Command

Laravel can display an overview of your application's configuration, drivers, and environment via the about Artisan command.


php artisan about

If you're only interested in a particular section of the application overview output, you may filter for that section using the --only option:


php artisan about --only=environment

Or, to explore a specific configuration file's values in detail, you may use the config:show Artisan command:


php artisan config:show database

Environment Configuration

It is often helpful to have different configuration values based on the environment where the application is running. For example, you may wish to use a different cache driver locally than you do on your production server.

To make this a cinch, Laravel utilizes the DotEnv PHP library. In a fresh Laravel installation, the root directory of your application will contain a .env.example file that defines many common environment variables. During the Laravel installation process, this file will automatically be copied to .env.

Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then read by the configuration files within the config directory using Laravel's env function.

If you are developing with a team, you may wish to continue including and updating the .env.example file with your application. By putting placeholder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.

[!NOTE]

Any variable in your .env file can be overridden by external environment variables such as server-level or system-level environment variables.

Environment File Security

Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.

However, it is possible to encrypt your environment file using Laravel's built-in environment encryption. Encrypted environment files may be placed in source control safely.

Additional Environment Files

Before loading your application's environment variables, Laravel determines if an APP_ENV environment variable has been externally provided or if the --env CLI argument has been specified. If so, Laravel will attempt to load an .env.[APP_ENV] file if it exists. If it does not exist, the default .env file will be loaded.

Environment Variable Types

All variables in your .env files are typically parsed as strings, so some reserved values have been created to allow you to return a wider range of types from the env() function:

| .env Value | env() Value |

| ------------ | ------------- |

| true         | (bool) true   |

| (true)       | (bool) true   |

| false        | (bool) false  |

| (false)      | (bool) false  |

| empty        | (string) ''   |

| (empty)      | (string) ''   |

| null         | (null) null   |

| (null)       | (null) null   |

If you need to define an environment variable with a value that contains spaces, you may do so by enclosing the value in double quotes:


APP_NAME="My Application"

Retrieving Environment Configuration

All of the variables listed in the .env file will be loaded into the $_ENV PHP super-global when your application receives a request. However, you may use the env function to retrieve values from these variables in your configuration files. In fact, if you review the Laravel configuration files, you will notice many of the options are already using this function:


'debug' => (bool) env('APP_DEBUG', false),

The second value passed to the env function is the "default value". This value will be returned if no environment variable exists for the given key.

Determining the Current Environment

The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the App facade:


use Illuminate\Support\Facades\App;



$environment = App::environment();

You may also pass arguments to the environment method to determine if the environment matches a given value. The method will return true if the environment matches any of the given values:


if (App::environment('local')) {

    // The environment is local

}



if (App::environment(['local', 'staging'])) {

    // The environment is either local OR staging...

}

[!NOTE]

The current application environment detection can be overridden by defining a server-level APP_ENV environment variable.

Encrypting Environment Files

Unencrypted environment files should never be stored in source control. However, Laravel allows you to encrypt your environment files so that they may safely be added to source control with the rest of your application.

Encryption

To encrypt an environment file, you may use the env:encrypt command:


php artisan env:encrypt

Running the env:encrypt command will encrypt your .env file and place the encrypted contents in an .env.encrypted file. The decryption key is presented in the output of the command and should be stored in a secure password manager. If you would like to provide your own encryption key you may use the --key option when invoking the command:


php artisan env:encrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

[!NOTE]

The length of the key provided should match the key length required by the encryption cipher being used. By default, Laravel will use the AES-256-CBC cipher which requires a 32 character key. You are free to use any cipher supported by Laravel's encrypter by passing the --cipher option when invoking the command.

If your application has multiple environment files, such as .env and .env.staging, you may specify the environment file that should be encrypted by providing the environment name via the --env option:


php artisan env:encrypt --env=staging

Readable Variable Names

When encrypting your environment file, you may use the --readable option to retain visible variable names while encrypting their values:


php artisan env:encrypt --readable

This will produce an encrypted file with the following format:


APP_NAME=eyJpdiI6...

APP_ENV=eyJpdiI6...

APP_KEY=eyJpdiI6...

APP_DEBUG=eyJpdiI6...

APP_URL=eyJpdiI6...

Using the readable format allows you to see which environment variables exist without exposing sensitive data. It also makes reviewing pull requests much easier since you can see which variables were added, removed, or renamed without needing to decrypt the file.

When decrypting environment files, Laravel automatically detects which format was used, so no additional options are needed for the env:decrypt command.

[!NOTE]

When using the --readable option, comments, and blank lines from the original environment file are not included in the encrypted output.

Decryption

To decrypt an environment file, you may use the env:decrypt command. This command requires a decryption key, which Laravel will retrieve from the LARAVEL_ENV_ENCRYPTION_KEY environment variable:


php artisan env:decrypt

Or, the key may be provided directly to the command via the --key option:


php artisan env:decrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

When the env:decrypt command is invoked, Laravel will decrypt the contents of the .env.encrypted file and place the decrypted contents in the .env file.

The --cipher option may be provided to the env:decrypt command in order to use a custom encryption cipher:


php artisan env:decrypt --key=qUWuNRdfuImXcKxZ --cipher=AES-128-CBC

If your application has multiple environment files, such as .env and .env.staging, you may specify the environment file that should be decrypted by providing the environment name via the --env option:


php artisan env:decrypt --env=staging

In order to overwrite an existing environment file, you may provide the --force option to the env:decrypt command:


php artisan env:decrypt --force

Accessing Configuration Values

You may easily access your configuration values using the Config facade or global config function from anywhere in your application. The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist:


use Illuminate\Support\Facades\Config;



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



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



// Retrieve a default value if the configuration value does not exist...

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

To set configuration values at runtime, you may invoke the Config facade's set method or pass an array to the config function:


Config::set('app.timezone', 'America/Chicago');



config(['app.timezone' => 'America/Chicago']);

To assist with static analysis, the Config facade also provides typed configuration retrieval methods. If the retrieved configuration value does not match the expected type, an exception will be thrown:


Config::string('config-key');

Config::integer('config-key');

Config::float('config-key');

Config::boolean('config-key');

Config::array('config-key');

Config::collection('config-key');

Configuration Caching

To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which can be quickly loaded by the framework.

You should typically run the php artisan config:cache command as part of your production deployment process. The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development.

Once the configuration has been cached, your application's .env file will not be loaded by the framework during requests or Artisan commands; therefore, the env function will only return external, system level environment variables.

For this reason, you should ensure you are only calling the env function from within your application's configuration (config) files. You can see many examples of this by examining Laravel's default configuration files. Configuration values may be accessed from anywhere in your application using the config function described above.

The config:clear command may be used to purge the cached configuration:


php artisan config:clear

[!WARNING]

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded; therefore, the env function will only return external, system level environment variables.

Configuration Publishing

Most of Laravel's configuration files are already published in your application's config directory; however, certain configuration files like cors.php and view.php are not published by default, as most applications will never need to modify them.

However, you may use the config:publish Artisan command to publish any configuration files that are not published by default:


php artisan config:publish



php artisan config:publish --all

Debug Mode

The debug option in your config/app.php configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the APP_DEBUG environment variable, which is stored in your .env file.

[!WARNING]

For local development, you should set the APP_DEBUG environment variable to true. In your production environment, this value should always be false. If the variable is set to true in production, you risk exposing sensitive configuration values to your application's end users.

Maintenance Mode

When your application is in maintenance mode, a custom view will be displayed for all requests into your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, a Symfony\Component\HttpKernel\Exception\HttpException instance will be thrown with a status code of 503.

To enable maintenance mode, execute the down Artisan command:


php artisan down

If you would like the Refresh HTTP header to be sent with all maintenance mode responses, you may provide the refresh option when invoking the down command. The Refresh header will instruct the browser to automatically refresh the page after the specified number of seconds:


php artisan down --refresh=15

You may also provide a retry option to the down command, which will be set as the Retry-After HTTP header's value, although browsers generally ignore this header:


php artisan down --retry=60

Bypassing Maintenance Mode

To allow maintenance mode to be bypassed using a secret token, you may use the secret option to specify a maintenance mode bypass token:


php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

After placing the application in maintenance mode, you may navigate to the application URL matching this token and Laravel will issue a maintenance mode bypass cookie to your browser:


https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515

If you would like Laravel to generate the secret token for you, you may use the with-secret option. The secret will be displayed to you once the application is in maintenance mode:


php artisan down --with-secret

When accessing this hidden route, you will then be redirected to the / route of the application. Once the cookie has been issued to your browser, you will be able to browse the application normally as if it was not in maintenance mode.

[!NOTE]

Your maintenance mode secret should typically consist of alpha-numeric characters and, optionally, dashes. You should avoid using characters that have special meaning in URLs such as ? or &.

Maintenance Mode on Multiple Servers

By default, Laravel determines if your application is in maintenance mode using a file-based system. This means to activate maintenance mode, the php artisan down command has to be executed on each server hosting your application.

Alternatively, Laravel offers a cache-based method for handling maintenance mode. This method requires running the php artisan down command on just one server. To use this approach, modify the maintenance mode variables in your application's .env file. You should select a cache store that is accessible by all of your servers. This ensures the maintenance mode status is consistently maintained across every server:


APP_MAINTENANCE_DRIVER=cache

APP_MAINTENANCE_STORE=database

Pre-Rendering the Maintenance Mode View

If you utilize the php artisan down command during deployment, your users may still occasionally encounter errors if they access the application while your Composer dependencies or other infrastructure components are updating. This occurs because a significant part of the Laravel framework must boot in order to determine your application is in maintenance mode and render the maintenance mode view using the templating engine.

For this reason, Laravel allows you to pre-render a maintenance mode view that will be returned at the very beginning of the request cycle. This view is rendered before any of your application's dependencies have loaded. You may pre-render a template of your choice using the down command's render option:


php artisan down --render="errors::503"

Redirecting Maintenance Mode Requests

While in maintenance mode, Laravel will display the maintenance mode view for all application URLs the user attempts to access. If you wish, you may instruct Laravel to redirect all requests to a specific URL. This may be accomplished using the redirect option. For example, you may wish to redirect all requests to the / URI:


php artisan down --redirect=/

Disabling Maintenance Mode

To disable maintenance mode, use the up command:


php artisan up

[!NOTE]

You may customize the default maintenance mode template by defining your own template at resources/views/errors/503.blade.php.

Maintenance Mode and Queues

While your application is in maintenance mode, no queued jobs will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.

Alternatives to Maintenance Mode

Since maintenance mode requires your application to have several seconds of downtime, consider running your applications on a fully-managed platform like Laravel Cloud to accomplish zero-downtime deployment with Laravel.

مقدمه (Introduction)

تمام فایل‌های پیکربندی (Configuration files) فریم‌ورک لاراول در دایرکتوری یا پوشه config ذخیره شده‌اند. تک‌تک گزینه‌ها مستندسازی شده‌اند، بنابراین راحت باشید و نگاهی به این فایل‌ها بیندازید تا با گزینه‌های در دسترس خود آشنا شوید.

این فایل‌های پیکربندی به شما اجازه می‌دهند مواردی مانند اطلاعات اتصال به پایگاه داده (Database connection information)، اطلاعات سرور ایمیل (Mail server information) و همچنین مقادیر مختلف و کلیدی دیگرِ پیکربندی مانند آدرس اینترنتی برنامه (Application URL) و کلید رمزگذاری (Encryption key) را تنظیم کنید.

دستور about (The about Command)

لاراول می‌تواند از طریق دستور آرتیسانِ about، یک نمای کلی از پیکربندی، درایورها و محیطِ (Environment) برنامه شما را نمایش دهد.

Bash

php artisan about

اگر تنها به بخش خاصی از خروجیِ نمای کلی برنامه علاقه‌مند هستید، می‌توانید با استفاده از گزینه only-- آن بخش را فیلتر کنید:

Bash

php artisan about --only=environment

یا برای بررسی دقیق و با جزئیاتِ مقادیر یک فایل پیکربندی خاص، می‌توانید از دستور آرتیسانِ config:show استفاده کنید:

Bash

php artisan config:show database

پیکربندی محیطی (Environment Configuration)

اغلب مفید است که بر اساس محیطی که برنامه در آن در حال اجراست، مقادیر پیکربندی (Configuration values) متفاوتی داشته باشید. به عنوان مثال، ممکن است بخواهید در سیستم محلی خود (Locally) از یک درایور کش (Cache driver) متفاوت نسبت به سرور اصلی و زنده (Production server) استفاده کنید.

برای اینکه این کار مثل آب خوردن ساده شود، لاراول از کتابخانه پی‌اچ‌پای DotEnv استفاده می‌کند. در یک نصب تازه و خام لاراول، دایرکتوری ریشه (Root) برنامه شما شامل یک فایل به نام .env.example خواهد بود که بسیاری از متغیرهای محیطی (Environment variables) رایج را تعریف می‌کند. در طول فرآیند نصب لاراول، این فایل به صورت خودکار در فایلی به نام .env کپی می‌شود.

فایل پیش‌فرض .env لاراول شامل برخی مقادیر پیکربندی رایج است که ممکن است بسته به اینکه برنامه شما روی یک سیستم محلی اجرا می‌شود یا یک سرور وب اصلی، متفاوت باشند. سپس این مقادیر توسط فایل‌های پیکربندی موجود در دایرکتوری config و با استفاده از تابع env لاراول خوانده می‌شوند.

اگر به صورت تیمی در حال توسعه هستید، ممکن است بخواهید به همراه برنامه خود، فایل .env.example را نیز قرار داده و آن را به‌روزرسانی کنید. با قراردادن مقادیر جای‌نما (Placeholder values) در فایل پیکربندی نمونه، سایر توسعه‌دهندگان تیم شما می‌توانند به وضوح ببینند که برای اجرای برنامه به کدام متغیرهای محیطی نیاز است.

[!NOTE]

نکته (Note)

هر متغیری در فایل .env شما می‌تواند توسط متغیرهای محیطی خارجی (External environment variables) مانند متغیرهای محیطی در سطح سرور (Server-level) یا سطح سیستم (System-level) بازنویسی (Override) شود.

امنیت فایل محیطی (Environment File Security)

فایل .env شما نباید به سیستم کنترل نسخه یا سورس کنترل (Source control) برنامه ارسال (Commit) شود؛ چرا که هر توسعه‌دهنده یا سروری که از برنامه شما استفاده می‌کند، ممکن است به پیکربندی محیطی (Environment configuration) متفاوتی نیاز داشته باشد. علاوه‌بر این، در صورت دسترسی یک مهاجم به مخزن سورس کنترل (Source control repository) شما، این امر یک ریسک امنیتی خواهد بود؛ زیرا تمام اطلاعات حساس و اطلاعات هویتی (Sensitive credentials) شما فاش خواهند شد.

با این حال، امکان رمزگذاری (Encrypt) فایل محیطی شما با استفاده از قابلیت داخلیِ رمزگذاری محیطی لاراول وجود دارد. فایل‌های محیطی رمزگذاری‌شده را می‌توان با امنیت کامل در سورس کنترل قرار داد.

فایل‌های محیطی اضافی (Additional Environment Files)

لاراول پیش از بارگذاری متغیرهای محیطی برنامه شما، ابتدا بررسی می‌کند که آیا متغیر محیطی APP_ENV به صورت خارجی (مثلاً در تنظیمات سرور) ارائه شده است یا اینکه آرگومان env-- در خط فرمان (CLI) مشخص شده است یا خیر. در این صورت، لاراول تلاش می‌کند تا فایل .env.[APP_ENV] را در صورت وجود بارگذاری کند. اگر این فایل وجود نداشته باشد، همان فایل پیش‌فرض .env بارگذاری خواهد شد.

انواع داده‌ای متغیرهای محیطی (Environment Variable Types)

تمام متغیرها در فایل‌های .env شما معمولاً به عنوان رشته (String) تجزیه و تفسیر می‌شوند؛ به همین دلیل، برخی مقادیر رزروشده (Reserved values) ایجاد شده‌اند تا به شما اجازه دهند طیف وسیع‌تری از انواع داده‌ای (Types) را از تابع env() دریافت کنید:

مقدار در env. مقدار خروجی ()env
true (bool) true
(true) (bool) true
false (bool) false
(false) (bool) false
empty (string) ''
(empty) (string) ''
null (null) null
(null) (null) null

اگر نیاز دارید متغیر محیطی‌ای تعریف کنید که مقدار آن شامل فاصله (Space) است، می‌توانید این کار را با قراردادن آن مقدار داخل علامت نقل‌قول دوتایی ("") انجام دهید:

تکه‌کد

APP_NAME="My Application"

بازیابی پیکربندی محیطی (Retrieving Environment Configuration)

هنگامی که برنامه شما یک درخواست (Request) دریافت می‌کند، تمام متغیرهای لیست شده در فایل .env در ابرمتغیرِ یا سوپرگلوبالِ $_ENV در پی‌اچ‌پای بارگذاری می‌شوند. با این حال، شما می‌توانید از تابع env برای بازیابی مقادیر این متغیرها در فایل‌های پیکربندی خود استفاده کنید. در واقع، اگر فایل‌های پیکربندی لاراول را بررسی کنید، متوجه خواهید شد که بسیاری از گزینه‌ها از قبل در حال استفاده از این تابع هستند:

PHP

'debug' => (bool) env('APP_DEBUG', false),

مقدار دومی که به تابع env پاس داده می‌شود، «مقدار پیش‌فرض» (Default value) است. این مقدار در صورتی بازگردانده می‌شود که هیچ متغیر محیطی برای کلید (Key) مشخص‌شده وجود نداشته باشد.

تشخیص محیط فعلی (Determining the Current Environment)

محیط فعلی برنامه (Application environment) بر اساس متغیر APP_ENV در فایل .env شما تعیین می‌شود. شما می‌توانید از طریق متد environment روی فساد App به این مقدار دسترسی داشته باشید:

PHP

use Illuminate\Support\Facades\App;

$environment = App::environment();

همچنین می‌توانید آرگومان‌هایی را به متد environment پاس دهید تا بررسی کنید آیا محیط برنامه با یک مقدار مشخص مطابقت دارد یا خیر. اگر محیط برنامه با هر یک از مقادیر داده‌شده همخوانی داشته باشد، این متد مقدار true را برمی‌گرداند:

PHP

if (App::environment('local')) {
    // محیط برنامه local است
}

if (App::environment(['local', 'staging'])) {
    // محیط برنامه یا local است یا staging...
}

[!NOTE]

نکته (Note)

فرآیند تشخیص محیط فعلی برنامه می‌تواند با تعریف یک متغیر محیطی APP_ENV در سطح سرور (Server-level)، بازنویسی و لغو شود .

رمزگذاری فایل‌های محیطی (Encrypting Environment Files)

فایل‌های محیطی رمزگذاری‌نشده هرگز نباید در سورس کنترل (Source control) ذخیره شوند. با این حال، لاراول به شما این امکان را می‌دهد که فایل‌های محیطی خود را رمزگذاری کنید تا بتوانید آن‌ها را با امنیت کامل در کنار بقیه بخش‌های برنامه خود به سورس کنترل اضافه کنید.

رمزگذاری (Encryption)

برای رمزگذاری یک فایل محیطی، می‌توانید از دستور env:encrypt استفاده کنید:

Bash

php artisan env:encrypt

اجرای دستور env:encrypt فایل .env شما را رمزگذاری کرده و محتوای رمزگذاری‌شده را در فایلی به نام .env.encrypted قرار می‌دهد. کلید رمزگشایی (Decryption key) در خروجی دستور نمایش داده می‌شود و باید در یک مدیریت کلمه عبور (Password manager) امن ذخیره شود. اگر مایلید کلید رمزگذاری اختصاصی خود را وارد کنید، می‌توانید هنگام اجرای دستور از گزینه key-- استفاده کنید:

Bash

php artisan env:encrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

[!NOTE]

نکته (Note)

طول کلید ارائه شده باید با طول کلید مورد نیازِ الگوریتم رمزگذاری (Encryption cipher) در حال استفاده مطابقت داشته باشد. به صورت پیش‌فرض، لاراول از الگوریتم AES-256-CBC استفاده می‌کند که به یک کلید ۳۲ کاراکتری نیاز دارد. شما آزادید با پاس دادن گزینه cipher-- هنگام اجرای دستور، از هر الگوریتم رمزگذاری دیگری که توسط بخش رمزگذار لاراول (Laravel's encrypter) پشتیبانی می‌شود، استفاده کنید.

اگر برنامه شما دارای چندین فایل محیطی است (مانند .env و .env.staging)، می‌توانید با ارائه نام محیط از طریق گزینه env--، فایل محیطی مشخصی را که باید رمزگذاری شود تعیین کنید:

Bash

php artisan env:encrypt --env=staging

نام‌های متغیر خوانا (Readable Variable Names)

هنگام رمزگذاری فایل محیطی خود، می‌توانید از گزینه readable-- استفاده کنید تا نام متغیرها قابل مشاهده باقی بمانند و فقط مقادیر آن‌ها رمزگذاری شوند:

Bash

php artisan env:encrypt --readable

این دستور یک فایل رمزگذاری‌شده با فرمت زیر تولید خواهد کرد:

تکه‌کد

APP_NAME=eyJpdiI6...
APP_ENV=eyJpdiI6...
APP_KEY=eyJpdiI6...
APP_DEBUG=eyJpdiI6...
APP_URL=eyJpdiI6...

استفاده از فرمت خوانا (Readable format) به شما اجازه می‌دهد بدون فاش شدن داده‌های حساس، ببینید چه متغیرهای محیطی‌ای در پروژه وجود دارند. این قابلیت همچنین بررسی پول ریکوئست‌ها (Pull Requests) را بسیار آسان‌تر می‌کند؛ زیرا می‌توانید بدون نیاز به رمزگشایی فایل، متوجه شوید کدام متغیرها اضافه، حذف یا تغییر نام داده شده‌اند.

هنگام رمزگشایی فایل‌های محیطی، لاراول به طور خودکار تشخیص می‌دهد که از کدام فرمت استفاده شده است، بنابراین برای اجرای دستور env:decrypt به هیچ گزینه اضافی نیاز ندارید.

[!NOTE]

نکته (Note)

هنگام استفاده از گزینه readable--، کامنت‌ها (توضیحات) و خطوط خالیِ فایل محیطی اصلی، در خروجی رمزگذاری‌شده قرار نخواهند گرفت .

رمزگشایی (Decryption)

برای رمزگشایی یک فایل محیطی، می‌توانید از دستور env:decrypt استفاده کنید. این دستور به یک کلید رمزگشایی نیاز دارد که لاراول آن را از متغیر محیطی LARAVEL_ENV_ENCRYPTION_KEY بازیابی می‌کند:

Bash

php artisan env:decrypt

یا اینکه می‌توانید کلید را به صورت مستقیم و از طریق گزینه key-- به دستور پاس دهید:

Bash

php artisan env:decrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

هنگامی که دستور env:decrypt فراخوانی می‌شود، لاراول محتویات فایل .env.encrypted را رمزگشایی کرده و محتوای رمزگشایی‌شده را در فایل .env قرار می‌دهد.

جهت استفاده از یک الگوریتم رمزگذاری سفارشی، می‌توان گزینه cipher-- را به دستور env:decrypt اضافه کرد:

Bash

php artisan env:decrypt --key=qUWuNRdfuImXcKxZ --cipher=AES-128-CBC

اگر برنامه شما دارای چندین فایل محیطی است (مانند .env و .env.staging)، می‌توانید با ارائه نام محیط از طریق گزینه env--، فایل محیطی مشخصی را که باید رمزگشایی شود تعیین کنید:

Bash

php artisan env:decrypt --env=staging

به منظور بازنویسی و جایگزینی روی یک فایل محیطیِ موجود، می‌توانید گزینه force-- را به دستور env:decrypt پاس دهید:

Bash

php artisan env:decrypt --force

دسترسی به مقادیر پیکربندی (Accessing Configuration Values)

شما می‌توانید به راحتی و از هر کجای برنامه خود، با استفاده از فساد Config یا تابع عمومی config به مقادیر پیکربندی خود دسترسی داشته باشید. دسترسی به این مقادیر با استفاده از نحو «نقطه‌ای» (Dot syntax) انجام می‌شود که شامل نام فایل و گزینه مورد نظر شماست. همچنین می‌توان یک مقدار پیش‌فرض نیز مشخص کرد تا در صورت عدم وجود گزینه پیکربندی مربوطه، آن مقدار بازگردانده شود:

PHP

use Illuminate\Support\Facades\Config;

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

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

// بازیابی یک مقدار پیش‌فرض در صورتی که گزینه پیکربندی وجود نداشته باشد...
$value = config('app.timezone', 'Asia/Seoul');

برای مقداردهی و تنظیم مقادیر پیکربندی در زمان اجرا (Runtime)، می‌توانید متد set از فساد Config را فراخوانی کنید یا یک آرایه را به تابع config پاس دهید:

PHP

Config::set('app.timezone', 'America/Chicago');

config(['app.timezone' => 'America/Chicago']);

برای کمک به تحلیل ایستای کد (Static Analysis)، فساد Config متدهایی را نیز برای بازیابی پیکربندی بر اساس نوع داده‌ای مشخص (Typed configuration retrieval) ارائه می‌دهد. اگر مقدار پیکربندی بازیابی‌شده با نوع داده‌ای مورد انتظار مطابقت نداشته باشد، یک استثنا (Exception) پرتاب خواهد شد:

PHP

Config::string('config-key');
Config::integer('config-key');
Config::float('config-key');
Config::boolean('config-key');
Config::array('config-key');
Config::collection('config-key');

کش کردن پیکربندی (Configuration Caching)

برای افزایش سرعت برنامه‌تان، باید تمام فایل‌های پیکربندی خود را با استفاده از دستور آرتیسان config:cache در یک فایل واحد کش (Cache) کنید. این دستور، تمام گزینه‌های پیکربندی برنامه شما را در یک فایل تک ترکیب می‌کند که توسط فریم‌ورک با سرعت بسیار بالا بارگذاری می‌شود.

به طور معمول، شما باید دستور php artisan config:cache را به عنوان بخشی از فرآیند استقرار در سرور اصلی (Production deployment) اجرا کنید. این دستور نباید در طول توسعه محلی (Local development) اجرا شود؛ چرا که گزینه‌های پیکربندی در طول مراحل توسعه برنامه شما به طور مکرر نیاز به تغییر خواهند داشت.

پس از کش شدن پیکربندی، فایل .env برنامه شما در طول درخواست‌ها یا دستورات آرتیسان توسط فریم‌ورک بارگذاری نخواهد شد؛ بنابراین، تابع env تنها متغیرهای محیطیِ خارجی و در سطح سیستم (System-level) را بازمی‌گرداند.

به همین دلیل، باید مطمئن شوید که تابع env را فقط از داخل فایل‌های پیکربندی (پوشه config) برنامه‌تان فراخوانی می‌کنید. شما می‌توانید نمونه‌های زیادی از این مورد را با بررسی فایل‌های پیکربندی پیش‌فرض لاراول مشاهده کنید. مقادیر پیکربندی را می‌توان از هر کجای برنامه با استفاده از تابع config (که پیش‌تر توضیح داده شد) فرخواند و به آن‌ها دسترسی داشت.

از دستور config:clear می‌توان برای پاکسازی و حذف پیکربندیِ کش‌شده استفاده کرد:

Bash

php artisan config:clear

[!WARNING]

هشدار (Warning)

اگر دستور config:cache را در طول فرآیند استقرار (Deployment) خود اجرا می‌کنید، باید کاملاً مطمئن باشید که تابع env را فقط و فقط از داخل فایل‌های پیکربندی خود فراخوانی کرده‌اید. پس از کش شدن پیکربندی، فایل .env بارگذاری نخواهد شد؛ در نتیجه، تابع env تنها متغیرهای محیطی خارجی و در سطح سیستم را باز می‌گرداند.

انتشار فایل‌های پیکربندی (Configuration Publishing)

بیشتر فایل‌های پیکربندی لاراول از قبل در دایرکتوری config برنامه شما منتشر (Publish) شده‌اند؛ با این حال، برخی از فایل‌های پیکربندی خاص مانند cors.php و view.php به صورت پیش‌فرض منتشر نمی‌شوند، چرا که اکثر برنامه‌ها هرگز نیازی به تغییر آن‌ها پیدا نخواهند کرد.

اما شما می‌توانید با استفاده از دستور آرتیسانِ config:publish، هر کدام از فایل‌های پیکربندی را که به صورت پیش‌فرض منتشر نشده‌اند، منتشر کنید:

Bash

php artisan config:publish

Bash

php artisan config:publish --all

حالت دیباگ یا عیب‌یابی (Debug Mode)

گزینه debug در فایل پیکربندی config/app.php تعیین می‌کند که چه مقدار اطلاعات درباره یک خطا واقعاً به کاربر نمایش داده شود. به صورت پیش‌فرض، این گزینه به‌گونه‌ای تنظیم شده است که از مقدار متغیر محیطی APP_DEBUG (که در فایل .env شما ذخیره می‌شود) پیروی کند.

[!WARNING]

هشدار (Warning)

برای توسعه محلی (Local development)، باید متغیر محیطی APP_DEBUG را روی true تنظیم کنید. در محیط سرور اصلی (Production environment)، این مقدار همیشه باید false باشد. اگر این متغیر در سرور اصلی روی true تنظیم شده باشد، این ریسک وجود دارد که مقادیر حساس پیکربندی پروژه خود را در معرض دید کاربران نهایی برنامه قرار دهید.

حالت تعمیر و نگهداری (Maintenance Mode)

هنگامی که برنامه شما در حالت تعمیر و نگهداری (Maintenance mode) قرار دارد، یک ویو یا نمای سفارشی برای تمام درخواست‌های ورودی به برنامه‌تان نمایش داده می‌شود. این قابلیت، «غیرفعال کردن» برنامه را در زمان به‌روزرسانی یا انجام کارهای تعمیراتی آسان می‌کند. بررسی حالت تعمیر و نگهداری به‌طور پیش‌فرض در پشته‌ی میان‌افزارهای (Middleware stack) برنامه شما گنجانده شده است؛ اگر برنامه در حالت تعمیر و نگهداری باشد، یک نمونه از استثنای Symfony\Component\HttpKernel\Exception\HttpException با استاتوس کد (Status code) ۵۰۳ پرتاب خواهد شد.

برای فعال‌سازی حالت تعمیر و نگهداری، دستور آرتیسان down را اجرا کنید:

Bash

php artisan down

اگر مایلید هدر HTTP از نوع Refresh به همراه تمام پاسخ‌های حالت تعمیر و نگهداری ارسال شود، می‌توانید هنگام فراخوانی دستور down گزینه refresh را ارائه دهید. هدر Refresh به مرورگر دستور می‌دهد که پس از تعداد ثانیه مشخص‌شده، صفحه را به طور خودکار بازنشانی (Refresh) کند:

Bash

php artisan down --refresh=15

همچنین می‌توانید گزینه retry را به دستور down پاس دهید تا به عنوان مقدار هدر HTTP از نوع Retry-After تنظیم شود؛ هرچند مرورگرها عموماً این هدر را نادیده می‌گیرند:

Bash

php artisan down --retry=60

دور زدن حالت تعمیر و نگهداری (Bypassing Maintenance Mode)

برای اینکه امکان دور زدن حالت تعمیر و نگهداری با استفاده از یک توکن مخفی (Secret token) فراهم شود، می‌توانید از گزینه secret برای مشخص کردن یک توکنِ دور زدن استفاده کنید:

Bash

php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

پس از قرار دادن برنامه در حالت تعمیر و نگهداری، می‌توانید به آدرس اینترنتی (URL) برنامه که با این توکن مطابقت دارد مراجعه کنید. با این کار، لاراول یک کوکیِ دور زدن حالت تعمیر و نگهداری (Maintenance mode bypass cookie) به مرورگر شما ارسال می‌کند:

Plaintext

https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515

اگر می‌خواهید لاراول خودش این توکن مخفی را برای شما تولید کند، می‌توانید از گزینه with-secret استفاده کنید. پس از قرار گرفتن برنامه در حالت تعمیر و نگهداری، این توکن مخفی به شما نمایش داده خواهد شد:

Bash

php artisan down --with-secret

هنگام دسترسی به این مسیر مخفی (Hidden route)، شما به مسیر اصلی برنامه (/) ریدایرکت (هدایت) خواهید شد. زمانی که کوکی به مرورگر شما ارسال شد، می‌توانید به طور عادی و به گونه‌ای که انگار برنامه در حالت تعمیر و نگهداری نیست، در سایت گشت‌وگذار کنید.

[!NOTE]

نکته (Note)

توکن مخفی حالت تعمیر و نگهداری شما معمولاً باید شامل کاراکترهای الفبایی-عددی (Alpha-numeric) و در صورت تمایل، خط تیره (-) باشد. باید از به‌کاربردن کاراکترهایی که در آدرس‌های اینترنتی (URLs) معنای خاصی دارند (مانند ? یا &) خودداری کنید

حالت تعمیر و نگهداری در چندین سرور (Maintenance Mode on Multiple Servers)

به صورت پیش‌فرض، لاراول با استفاده از یک سیستم مبتنی بر فایل (File-based system) تشخیص می‌دهد که آیا برنامه شما در حالت تعمیر و نگهداری قرار دارد یا خیر. این یعنی برای فعال‌سازی حالت تعمیر و نگهداری، دستور php artisan down باید روی تک‌تک سرورهایی که میزبان برنامه شما هستند اجرا شود.

به عنوان روش جایگزین، لاراول یک متد مبتنی بر کش (Cache-based method) را برای مدیریت حالت تعمیر و نگهداری ارائه می‌دهد. این روش تنها به اجرای دستور php artisan down روی یکی از سرورها نیاز دارد. برای استفاده از این راهکار، متغیرهای حالت تعمیر و نگهداری را در فایل .env برنامه خود تغییر دهید. شما باید یک مخزن کش (Cache store) را انتخاب کنید که توسط همه سرورهای شما قابل دسترسی باشد. این کار تضمین می‌کند که وضعیت حالت تعمیر و نگهداری به صورت یکپارچه در تمام سرورها برقرار و حفظ شود:

تکه‌کد

APP_MAINTENANCE_DRIVER=cache
APP_MAINTENANCE_STORE=database

پیش‌رندر کردن ویو حالت تعمیر و نگهداری (Pre-Rendering the Maintenance Mode View)

اگر در طول فرآیند استقرار (Deployment) از دستور php artisan down استفاده کنید، چنانچه کاربران در زمان به‌روزرسانی وابستگی‌های کامپوزر (Composer dependencies) یا سایر اجزای زیرساختی به برنامه دسترسی پیدا کنند، ممکن است همچنان گهگاه با خطا مواجه شوند. این اتفاق به این دلیل رخ می‌دهد که بخش قابل‌توجهی از فریم‌ورک لاراول باید بوت (Boot) و راه‌اندازی شود تا بتواند تشخیص دهد برنامه در حالت تعمیر و نگهداری است و سپس ویو مربوطه را با استفاده از موتور رندر قالب (Templating engine) نمایش دهد.

به همین دلیل، لاراول به شما این امکان را می‌دهد که یک ویو یا نمای حالت تعمیر و نگهداری را «پیش‌رندر» (Pre-render) کنید تا در همان ابتدای چرخه‌ی درخواست (Request cycle) بازگردانده شود. این ویو قبل از بارگذاری هرگونه وابستگی در برنامه‌ی شما رندر می‌شود. شما می‌توانید قالب دلخواه خود را با استفاده از گزینه render در دستور down پیش‌رندر کنید:

Bash

php artisan down --render="errors::503"

ریدایرکت کردن درخواست‌ها در حالت تعمیر و نگهداری (Redirecting Maintenance Mode Requests)

لاراول در حالت تعمیر و نگهداری، نمای این حالت را برای تمام آدرس‌های اینترنتی (URLs) برنامه که کاربر قصد دسترسی به آن‌ها را دارد نمایش می‌دهد. در صورت تمایل، می‌توانید به لاراول دستور دهید که تمام درخواست‌ها را به یک آدرس مشخص هدایت (Redirect) کند. این کار با استفاده از گزینه redirect امکان‌پذیر است. به عنوان مثال، ممکن است بخواهید تمام درخواست‌ها را به مسیر ریشه (/) ریدایرکت کنید:

Bash

php artisan down --redirect=/

غیرفعال‌سازی حالت تعمیر و نگهداری (Disabling Maintenance Mode)

برای غیرفعال کردن حالت تعمیر و نگهداری، از دستور up استفاده کنید:

Bash

php artisan up

[!NOTE]

نکته (Note)

شما می‌توانید قالب پیش‌فرض حالت تعمیر و نگهداری را با تعریف قالب اختصاصی خود در مسیر resources/views/errors/503.blade.php سفارشی‌سازی کنید.

حالت تعمیر و نگهداری و صف‌ها (Maintenance Mode and Queues)

زمانی که برنامه شما در حالت تعمیر و نگهداری قرار دارد، هیچ‌کدام از جاب‌های صف‌بندی‌شده (Queued jobs) پردازش نخواهند شد. به محض اینکه برنامه از حالت تعمیر و نگهداری خارج شود، پردازش جاب‌ها طبق روال عادی ادامه می‌یابد.

جایگزین‌های حالت تعمیر و نگهداری (Alternatives to Maintenance Mode)

از آنجایی که حالت تعمیر و نگهداری نیازمند چند ثانیه توقف و از دسترس خارج شدن برنامه (Downtime) است، می‌توانید برای رسیدن به استقرار بدون توقف (Zero-downtime deployment) در لاراول، اجرای برنامه‌های خود را روی پلتفرم‌های تمام‌مدیریت‌شده‌ای مانند Laravel Cloud در نظر بگیرید.