Laravel Configuration
Introduction
All of the configuration files for the Laravel framework are stored in the app/config directory. Each option in every file is documented, so feel free to look through the files and get familiar with the options available to you.
Sometimes you may need to access configuration values at run-time. You may do so using the Config class:
Accessing A Configuration Value
Config::get('app.timezone');
You may also specify a default value to return if the configuration option does not exist:
$timezone = Config::get('app.timezone', 'UTC');
Setting A Configuration Value
Notice that "dot" style syntax may be used to access values in the various files. You may also set configuration values at run-time:
Config::set('database.default', 'sqlite');
Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.
Environment Configuration
It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver on your local development machine than on the production server. It is easy to accomplish this using environment based configuration.
Simply create a folder within the config directory that matches your environment name, such as local. Next, create the configuration files you wish to override and specify the options for that environment. For example, to override the cache driver for the local environment, you would create a cache.php file in app/config/local with the following content:
<?php
return array(
'driver' => 'file',
);
Note: Do not use 'testing' as an environment name. This is reserved for unit testing.
Notice that you do not have to specify every option that is in the base configuration file, but only the options you wish to override. The environment configuration files will "cascade" over the base files.
Next, we need to instruct(指导;通知;命令;教授) the framework how to determine which environment it is running in. The default environment is always production. However, you may setup other environments within the bootstrap/start.php file at the root of your installation. In this file you will find an $app->detectEnvironment call. The array passed to this method is used to determine the current environment. You may add other environments and machine names to the array as needed.
<?php
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
));
In this example, 'local' is the name of the environment and 'your-machine-name' is the hostname of your server. On Linux and Mac, you may determine your hostname using the hostname terminal command.
If you need more flexible environment detection, you may pass a Closure to the detectEnvironment method, allowing you to implement environment detection however you wish:
$env = $app->detectEnvironment(function()
{
return $_SERVER['MY_LARAVEL_ENV'];
});
Accessing The Current Application Environment
You may access the current application environment via the environment method:
$environment = App::environment();
You may also pass arguments to the environment method to check if the 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...
}
Provider Configuration
When using environment configuration, you may want to "append" environment service providers to your primary app configuration file. However, if you try this, you will notice the environment app providers are overriding the providers in your primary app configuration file. To force the providers to be appended, use the append_config helper method in your environment app configuration file:
'providers' => append_config(array(
'LocalOnlyServiceProvider',
))
Protecting Sensitive Configuration
For "real" applications, it is advisable to keep all of your sensitive configuration out of your configuration files. Things such as database passwords, Stripe API keys, and encryption keys should be kept out of your configuration files whenever possible. So, where should we place them? Thankfully, Laravel provides a very simple solution to protecting these types of configuration items using "dot" files.
First, configure your application to recognize your machine as being in the local environment. Next, create a .env.local.php file within the root of your project, which is usually the same directory that contains your composer.json file. The .env.local.php should return an array of key-value pairs, much like a typical Laravel configuration file:
<?php
return array(
'TEST_STRIPE_KEY' => 'super-secret-sauce',
);
All of the key-value pairs returned by this file will automatically be available via the $_ENV and $_SERVERPHP "superglobals". You may now reference these globals from within your configuration files:
'key' => $_ENV['TEST_STRIPE_KEY']
Be sure to add the .env.local.php file to your .gitignore file. This will allow other developers on your team to create their own local environment configuration, as well as hide your sensitive configuration items from source control.
Now, on your production server, create a .env.php file in your project root that contains the corresponding values for your production environment. Like the .env.local.php file, the production .env.php file should never be included in source control.
Note: You may create a file for each environment supported by your application. For example, the
developmentenvironment will load the.env.development.phpfile if it exists. However, theproductionenvironment always uses the.env.phpfile.
Maintenance Mode
When your application is in maintenance mode, a custom view will be displayed for all routes into your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A call to the App::down method is already present in your app/start/global.php file. The response from this method will be sent to users when your application is in maintenance mode.
To enable maintenance mode, simply execute the down Artisan command:
php artisan down
To disable maintenance mode, use the up command:
php artisan up
To show a custom view when your application is in maintenance mode, you may add something like the following to your application's app/start/global.php file:
App::down(function()
{
return Response::view('maintenance', array(), 503);
});
If the Closure passed to the down method returns NULL, maintenance mode will be ignored for that request.
Maintenance Mode & Queues
While your application is in maintenance mode, no queue jobs will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.
Laravel Configuration的更多相关文章
- 优雅的使用 PhpStorm 来开发 Laravel 项目
[目录] Prerequisites plugin installation and configuration 1 Ensure Composer is initialized 2 Install ...
- [php]laravel框架容器管理的一些要点
本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...
- Centos7 编译安装 Nginx PHP Mariadb Memcached 扩展 ZendOpcache扩展 (实测 笔记 Centos 7.3 + Mariadb 10.1.20 + Nginx 1.10.2 + PHP 7.1.0 + Laravel 5.3 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7-x86_64-Minimal-1611.iso 安装步骤: 1.准备 1.0 查看硬 ...
- laravel/lumen 单元测试
Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...
- laravel administrator 一款通用的后台插件(PHP框架扩展)
前几天我看了一下zend framework 2的一些官方文档,也找了一些例子,可惜所有的资料少之甚少.于是我就开始去找这国外用的比较流行的PHP框架laravel,希望能够找到其合适的例子,而且我本 ...
- Laravel 之Service Providers
Service providers are the central place of all Laravel application bootstrapping. Your own applicati ...
- Laravel错误与日志处理
App\Exceptions\Handler class is where all exceptions triggered by your application are logged and th ...
- SmartWiki开发日记之Laravel缓存扩展
SmartWiki简介请阅读: http://www.cnblogs.com/lifeil/p/6113323.html 因为SmartWiki的演示站点部署在阿里云上,阿里云有一个128M免费的Me ...
- laravel小抄
原文地址:http://cheats.jesse-obrien.ca/ Artisan // Displays help for a given command php artisan --help ...
随机推荐
- WINCE 开机自动弹USB连接窗口问题(已解决)
平台:S3C6410+WinCE6.0 问题现像: 当启用UART0也就是COM1时,无论插没插USB线,一开机,它都会弹出个USB连接的窗口, 原因分析: 是因为在三星原版本BSP里的串口驱动上制定 ...
- Android开发之权限列表
权限定义 功能 android.permission.ACCESS_CHECKIN_PROPERTIES 允许读写访问"properties"表在checkin数据库中,改值可以修 ...
- ASP.NET Web API上实现 Web Socket
1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...
- npm安装插件提示
现在使用node的人群越来越多,咱也不能落后,得跟紧脚步才行. 今天使用 npm 安装插件的时候,出现以下提示,而且,安装程序看着好像终止了.而且,看到一个exit单词,就认为说安装出错了. $ np ...
- Qt之自定义界面(窗体缩放-跨平台终极版)
简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...
- ASP.NET MVC 学习2、从Controller传递数据到View
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view 一,Control ...
- 无法连接到SQL Server 2008 R2
服务器环境: 操作系统 名称: Microsoft Windows Server 2008 R2 Enterprise 版本: 6.1.7601 服务包: Ser ...
- [020] Android模拟器访问本地Web应用
本篇文章试图解决这样一个问题:如何在Android模拟器上访问本地的Web应用? 例如,在你的开发机器上启动一个Tomcat服务,接着打开电脑上的浏览器,默认情况下输入http://localhost ...
- mac里vmware fusion桥接设置
1 桥接模式网络连接:选择以太网 2 在虚拟机操作系统里设置好ip,手动设置,将虚拟机当成一台存在的物理机设置 3 在宿主MAC偏好设置-网络-以太网-高级-硬件, 配置:自动改为 手工, 速度:选择 ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...