Make Artisan Command Available if Debug Mode is Enabled in Laravel 10

Make Artisan Command Available if Debug Mode is Enabled in Laravel 10

In Laravel development, Artisan commands are a powerful tool for executing various tasks within the application. In some cases, you might want to make Artisan commands available only when the debug mode is enabled. This can be particularly useful when you want to restrict access to certain commands in production environments, ensuring a more secure and controlled application environment. This tutorial explains how to make Artisan command available if debug mode is enabled in Laravel 10.

Use the APP_DEBUG setting in the .env file to turn debug mode on or off.

.env

APP_DEBUG=true

The isEnabled method can be used for determining whether the command should be available. In this example, it returns the result of the hasDebugModeEnabled method, ensuring the command is only accessible when the application is in debug mode.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;

class TestCommand extends Command
{
    protected $signature = 'app:test';

    public function isEnabled(): bool
    {
        return App::hasDebugModeEnabled();
    }

    public function handle(): int
    {
        return Command::SUCCESS;
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.