Lumen/Laravel .env 파일의 환경 변수의 효율성에 대해 토론합니다.
.env 파일은 다른 유효한 환경 변수로 사용자 정의할 수 있으며 변수는 env() 또는 $_SERVER 또는 $_ENV을 호출하여 얻을 수 있습니다. 그렇다면 env()는 이러한 변수에 어떻게 로드됩니까? Lumen의 Vendor/laravel/lumen-framework/src/helpers.php에서 env 함수가 다음과 같이 정의되어 있음을 확인할 수 있습니다.
if (! function_exists('env')) {/** * Gets the value of an environment variable. Supports boolean, empty and null. * * @param string $key * @param mixed $default * @return mixed */function env($key, $default = null) {$value = getenv($key);if ($value === false) {return value($default); }switch (strtolower($value)) {case 'true':case '(true)':return true;case 'false':case '(false)':return false;case 'empty':case '(empty)':return '';case 'null':case '(null)':return; }if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {return substr($value, 1, -1); }return $value; } }
env 함수에서 환경을 읽기 위해 getenv()가 호출되는 것을 볼 수 있습니다. 변수. getenv()는 $_SERVER 또는 $_ENV 전역 변수를 읽기 위해 PHP가 기본적으로 제공하는 함수 API라는 것도 알고 있습니다. 왜 .env 파일의 환경 변수를 getenv()를 통해 얻을 수 있나요? vlucas/phpdotenv는 Lumen 또는 Laravel 공급업체에서 찾을 수 있습니다. 별도로 다운로드하려면 여기로 이동하세요.
vlucas/phpdotenv/src/Loader.php 파일에서 .env가 배열에 로드된 다음 각 줄이 setter로 처리되고 setEnvironmentVariable() 메서드가 호출되는 것을 볼 수 있습니다.
/** * Load `.env` file in given directory. * * @return array */public function load() {$this->ensureFileIsReadable();$filePath = $this->filePath;$lines = $this->readLinesFromFile($filePath);foreach ($lines as $line) {if (!$this->isComment($line) && $this->looksLikeSetter($line)) {$this->setEnvironmentVariable($line);} }return $lines; }/** * Read lines from the file, auto detecting line endings. * * @param string $filePath * * @return array */protected function readLinesFromFile($filePath) {// Read file into an array of lines with auto-detected line endings$autodetect = ini_get('auto_detect_line_endings');ini_set('auto_detect_line_endings', '1');$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);ini_set('auto_detect_line_endings', $autodetect);return $lines; }
Loader::setEnvironmentVariable($name, $value = null)은 다음과 같이 정의됩니다.
/** * Set an environment variable. * * This is done using: * - putenv, * - $_ENV, * - $_SERVER. * * The environment variable value is stripped of single and double quotes. * * @param string $name * @param string|null $value * * @return void */public function setEnvironmentVariable($name, $value = null) {list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);// Don't overwrite existing environment variables if we're immutable // Ruby's dotenv does this with `ENV[key] ||= value`.if ($this->immutable && $this->getEnvironmentVariable($name) !== null) {return; }// If PHP is running as an Apache module and an existing // Apache environment variable exists, overwrite itif (function_exists('apache_getenv') && function_exists('apache_setenv') && apache_getenv($name)) { apache_setenv($name, $value); } if (function_exists('putenv')) { putenv("$name=$value"); } $_ENV[$name] = $value; $_SERVER[$name] = $value;}
PHP tenv 그녀는 누구입니까?
Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically. This is a PHP version of the original Ruby dotenv.
왜 .env인가요?
You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables. Basically, a .env file is an easy way to load custom configuration variables that your application needs without having to modify .htaccess files or Apache/nginx virtual hosts. This means you won't have to edit any files outside the project, and all the environment variables are always set no matter how you run your project - Apache, Nginx, CLI, and even PHP 5.4's built-in webserver. It's WAY easier than all the other ways you know of to set environment variables, and you're going to love it. . NO editing virtual hosts in Apache or Nginx . NO adding php_value flags to .htaccess files . EASY portability and sharing of required ENV values . COMPATIBLE with PHP's built-in web server and CLI runner
위 내용은 Lumen/Laravel .env 파일의 환경 변수의 효율성에 대해 토론합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Laravel 이메일 전송이 실패 할 때 반환 코드를 얻는 방법. Laravel을 사용하여 응용 프로그램을 개발할 때 종종 확인 코드를 보내야하는 상황이 발생합니다. 그리고 실제로 ...

laravel 일정 작업 실행 비 응답 문제 해결 Laravel의 일정 작업 일정을 사용할 때 많은 개발자 가이 문제에 직면합니다 : 스케줄 : 실행 ...

Laravel의 이메일을 처리하지 않는 방법은 LaRavel을 사용하는 것입니다.

DCAT를 사용할 때 DCATADMIN (LARAVEL-ADMIN)에서 데이터를 추가하려면 사용자 정의의 테이블 기능을 구현하는 방법 ...

Laravel 프레임 워크 및 Laravel 프레임 워크 및 Redis를 사용할 때 Redis 연결을 공유하는 데 영향을 줄 수 있습니다. 개발자는 문제가 발생할 수 있습니다. 구성을 통해 ...

Laravel 다중 테넌트 확장 패키지 패키지 패키지 패키지 패키지 Stancl/Tenancy, ...

Laraveleloquent 모델 검색 : 데이터베이스 데이터를 쉽게 얻을 수 있습니다. 이 기사는 데이터베이스에서 데이터를 효율적으로 얻는 데 도움이되는 다양한 웅변 모델 검색 기술을 자세히 소개합니다. 1. 모든 기록을 얻으십시오. 모든 () 메소드를 사용하여 데이터베이스 테이블에서 모든 레코드를 가져옵니다. 이것은 컬렉션을 반환합니다. Foreach 루프 또는 기타 수집 방법을 사용하여 데이터에 액세스 할 수 있습니다 : Foreach ($ postas $ post) {echo $ post->

Laravel 데이터베이스 마이그레이션 중 중복 클래스 정의 문제가 발생합니다. 데이터베이스 마이그레이션에 Laravel 프레임 워크를 사용하는 경우 개발자가 "클래스가 사용되었습니다 ...
