Laravel Add Custom Configuration File Example (ok)

https://www.itsolutionstuff.com/post/laravel-add-custom-configuration-file-exampleexample.html

C:\xampp\htdocs\reset.env

APP_NAME=Test
APP_ENV=local
APP_KEY=base64:juFjkVpum8P2+53fXpeGT5aUGG05u4nU9Qo63OjZmac=
APP_DEBUG=true
APP_URL=http://test.test/reset
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reset
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=database
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=phamngoctuong1805@gmail.com
MAIL_PASSWORD=lufcvcbdnbjcrvta
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=phamngoctuong1805@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
GOOGLE_API_TOKEN=xxxxxxxxx

C:\xampp\htdocs\reset\config\google.php

<?php
return [
  'api_token' => env('GOOGLE_API_TOKEN', 'your-some-default-value'),
];

C:\xampp\htdocs\reset\routes\web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
use App\Http\Controllers\HomeController;
Route::get('/', function () {
  dd('Welcome to simple user route file.');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('helper', function () {
  $googleAPIToken = config('google.api_token');
  dd($googleAPIToken);
});

Laravel Add Custom Configuration File Example

you can easily create custom configuration file in laravel 6, laravel 7, laravel 8 and laravel 9 version.

in this example, we will create google.php config file and set api key on there. then we will simply get value using config() helper. let's see example.

Example

now first add your new variable on env file as like bellow:

.env

...  GOOGLE_API_TOKEN=xxxxxxxx

config/google.php

<?php  return [    'api_token' => env('GOOGLE_API_TOKEN', 'your-some-default-value'),];  

Use it:

Route::get('helper', function(){    $googleAPIToken = config('google.api_token');          dd($googleAPIToken);  });

Output:

Read Also: How to Add Custom env Variables in Laravel?

xxxxxxxx

now you can check your own.

i hope it can help you...

Last updated