Pop Console Component for Pop PHP Framework

3.2.0 2021-02-12 22:16 UTC

This package is auto-updated.

Last update: 2021-09-12 23:30:36 UTC


Build Status Coverage Status


pop-console provides a layer to run an application from the console window. It has support for input commands and options as well ANSI-based console colors. It can be easily be used with an application built with Pop to route requests from the CLI to the application.

pop-console is a component of the Pop PHP Framework.


Install pop-console using Composer.

composer require popphp/pop-console


Outputting to the console

You can use a console object to manage and deploy output to the console, including a prepended header and appended footer.

$console = new Pop\Console\Console();
$console->setHeader('My Application');
$console->setFooter('The End');

$console->append('Here is some console information.');
$console->append('Hope you enjoyed it!');


The above will output:

    My Application
    Here is some console information.
    Hope you enjoyed it!

    The End

Console colors

On consoles that support it, you can colorize text outputted to the console with the colorize() method:

    'Here is some ' . 
    $console->colorize('IMPORTANT', Console::BOLD_RED) .
    ' console information.'

Using a prompt

You can also trigger a prompt to get information from the user. You can enforce a certain set of options as well as whether or not they are case-sensitive:

$console = new Pop\Console\Console();
$letter  = $console->prompt(
    'Which is your favorite letter: A, B, C, or D? ',
    ['A', 'B', 'C', 'D'],
echo 'Your favorite letter is ' . $letter . '.';
Which is your favorite letter: A, B, C, or D? B   // <- User types 'B'
Your favorite letter is B.

Help screen

You can register commands with the console object to assist in auto-generating a well-formatted, colorized help screen.

use Pop\Console\Console;
use Pop\Console\Command;

$edit = new Command(
    'user edit', '<id>', 'This is the help for the user edit command'

$remove = new Command(
    'user remove', '<id>', 'This is the help for the user remove command'

$console = new Console();

Once the commands are registered with the main $console object, we can generate the help screen like this:


The above command will output an auto-generated, colorized help screen with the commands that are registered with the console object.


These are basic examples. Ideally, you could wire an application to use the console but not for setting routes, controllers and actions. Refer to the Pop PHP Tutorial example application to see how to wire up a CLI-based application using Pop PHP.