In Laravel, we have options to mark as our project is in development mode or production mode which is easy to stack trace errors or what else we can do. In developments mode we don't want to show something detail in public, and this we can do in Laravel project to check whether we want to show in public or in local developments mode.
This tutorial today, we will explain and show examples how to check running app environment. If you are looking for solution to check your environment app is running and this tutorial we will provide example with solution in controller file and blade view as well. We will go into detail how to check the current environment running in Laravel app. Within this example below you can use with versions Laravel 6, Laravel 7, Laravel 8, and Laravel 9.
Before go to example code, we would introduce you a file in Laravel root directory which called .env file which help you to enable the mode. You may see a configure name APP_ENV=local
or APP_ENV=production
. These setting will help you to switch between production mode and local mode. You have to modified the setting to preferred mode after implement below code.
All the last step, you have to clear configure with artisan command after changed .env file
php artisan config:clear
So let's see below examples to implement how to check app running environment in Laravel project.
We can use if-else statement to execute which block of statements in code to run in production mode.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
class ArticleController extends Controller
{
public function index()
{
if (App::environment(['production']))
{
dd("In production");
}
}
}
Another example using facade method to check environment to run block of statements.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function index()
{
if (app()->environment(['local', 'staging']))
{
dd("In local or staging.");
}
}
}
We also can check environment in blade file with helper. Example below the block of statements will execute when you configure as in production mode.
<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
@if(App::environment('production'))
<!-- You code to run in production -->
@endif
</body>
</html>
Another option you can use directive @production
to open and end the block of production to be execute.
<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
@production
<!--You code here -->
@endproduction
</body>
</html>
For this option which you can check in production or staging mode by pass the value into parenthesis.
<!doctype html>
<html>
<head>
<title>Test Blade File</title>
</head>
<body>
@env('local', 'staging')
<!-- You code Here -->
@endenv
</body>
</html>
Hope this article will help you to check app running in development or production mode. Have a nice day!!
You may also like...
Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him