In this article, We'll talk about the demonstration of the Laravel check app environment. You will learn how to check the application environment in Laravel with several different examples. This article provides many simple examples of checking the environment in which a laravel application is running.
For example, you want to check your Laravel application running in a staging or production environment. So there are many ways to check this. We will use App::environment()
, app()->environment()
, @production
and @env
to check app current env. So let's check one by one example as below.
How to Check Running Laravel App Environment
Example 1:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
if (App::environment(['local', 'staging'])) {
return "This is Local or Staging App";
}
}
}
Example 2:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
if (app()->environment(['production'])) {
return "This is a production app.";
}
}
}
Example 3:
@if(App::environment('production'))
<h1>Production Environment</h1>
@endif
Example 4:
@production
<h1>Production Environment</h1>
@endproduction
Example 5:
@env('local', 'staging')
<h1>Local or Staging Environment</h1>
@endenv
Thank you for reading this article.
Read Also: How to Define Constant Variables in Laravel
If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.