'How to hide some views only in production env Laravel

I am pretty new to Laravel. So I have a Laravel Vue app where i want to hide a view.blade only in my production environment and not on staging , i tried with the config file view but i didn't succeed. Can anyone help me please ? Thank you



Solution 1:[1]

One possible solution would be to use an if statement for the view. It's difficult to give a more contextual answer without more context.

example below:

<template>
   <view v-if="inProduction"></view>
</template>

export default {
   computed: {
      inProduction(){
         return process.env.NODE_ENV === "production";
      }
   }
}

Solution 2:[2]

You could do a conditional route middleware too so it just checks the config file (cached) for the env. Then you can send them anywhere you want if someone tried to hit that route.

Solution 3:[3]

If you're trying to hide a full route on certain env best way would be to create a Middleware like this:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AppEnv
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next, $env)
    {
        if(config('app.env') == $env) {
            return $next($request);
        }
        abort(404);
    }
}

You'll have to register this middleware on the app/Http/Kernel $routeMiddleware array

Then you can use it like this:

Route::view('/', 'welcome')->middleware(['env:local'])

(Change local for the env where you want to show this route)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maybeolivier
Solution 2
Solution 3 DharmanBot