Reputation: 73
I am using a Laravel 7.0. I am looking for a good way to display debug pages in only Debug environment? How should I do to hide in production environment?
Route::resource('/debug', 'DebugController')->middleware('auth');
Upvotes: 1
Views: 91
Reputation: 8242
If you have multiple controllers / resources that you want to apply this check you could create a middleware and apply it in the routes.
Something like this:
Route::resource('/debug', 'DebugController')->middleware(['auth', 'debug');
If you need this check only in this controller you could do a simple middleware closure in the constructor instead.
Something like this:
use Illuminate\Http\Response;
class DebugController extends Controller
{
public function __construct()
{
$this->middleware(function ($request, $next) {
if (app()->environment() === 'production') {
abort(Response::HTTP_FORBIDDEN);
}
return $next($request);
});
}
}
This would return a 403 response if the environment is production
but it would let the request through in development
, local
, testing
etc.
Upvotes: 1