greabock/console
greabock/console adds a lightweight console layer for Laravel/PHP apps, providing helpers to define and run CLI commands more cleanly. Useful for organizing command code and simplifying input/output handling in custom tooling and scripts.
Installation:
composer require greabock/console
Publish the config file (if needed):
php artisan vendor:publish --provider="Greabock\Console\ConsoleServiceProvider"
Basic Usage:
resources/views/layouts/app.blade.php):
@include('console::console')
First Use Case:
echo "Hello, Console!";
Debugging on the Fly:
dd() or dump() calls with console commands during development.dd($user), run:
var_dump($user);
in the browser console.Dynamic Data Inspection:
$posts = \App\Models\Post::all();
$posts->where('published', true)->count();
Testing Frontend Logic:
$response = \Http::post('api/endpoint', ['key' => 'value']);
json_encode($response->json());
Integration with Laravel Mix/Vite:
@include('console::console') directive is placed in your main layout file (e.g., resources/js/app.js or resources/views/app.blade.php).Conditional Execution:
if (app()->environment('local')) {
\Log::debug('Running in local environment');
}
Autoloading Classes:
$user = \App\Models\User::find(1);
$user->name;
Error Handling:
try {
$riskyOperation();
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
Performance Testing:
$start = microtime(true);
// Code to test
$time = microtime(true) - $start;
echo "Execution time: " . $time . "s";
Security Risks:
app()->environment('local')) or IP whitelisting.Performance Overhead:
Namespace Conflicts:
User instead of \App\Models\User), the console may resolve them incorrectly.use App\Models\User;
Session/State Persistence:
session(['console_data' => $data]);
Dependency Injection:
Auth, Cache) may not behave as expected in the console due to missing context.$auth = app('auth');
Console Not Showing?:
@include('console::console') is in your Blade template.PHP Errors:
try-catch blocks to handle errors gracefully.Clear Cache:
php artisan view:clear
php artisan cache:clear
Custom Console Commands:
php artisan vendor:publish --tag=console-views
resources/views/vendor/console/console.blade.php to add custom UI elements or logic.Hooks/Events:
EventServiceProvider:
protected $listen = [
'console.executed' => [YourListener::class],
];
Middleware Integration:
Route::middleware(['web', 'auth'])->group(function () {
// Console routes
});
Logging Console Output:
\Log::info('Console output: ' . $yourVariable);
How can I help you explore Laravel packages today?