Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Using AbstractConsoleController and ConsoleModel

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on November 29, 2014

zf2-zendframework2 When using console in ZF2 application, we can extends AbstractConsoleController instead of AbstractActionController. There is overridden dispatch() function in AbstractConsoleController which handle access that doesn’t come from console which is tricked like this :

// ...
    /**
     * {@inheritdoc}
     */
    public function dispatch(RequestInterface $request, ResponseInterface $response = null)
    {
        if (! $request instanceof ConsoleRequest) {
            throw new InvalidArgumentException(sprintf(
                '%s can only dispatch requests in a console environment',
                get_called_class()
            ));
        }
        return parent::dispatch($request, $response);
    }
// ...

So, by extending it, we can reduce its checking. So, our controller will look like the following :

namespace TutorialConsoleModule\Controller;

use Zend\Mvc\Controller\AbstractConsoleController;

class TutorialConsoleController extends AbstractConsoleController
{
    public function showDataAction()
    {
        // ...
    }
}

Now, we need to show something in console, instead of using Response object, we can use ConsoleModel so we can return ConsoleModel that setted with the data like this :

namespace TutorialConsoleModule\Controller;

use Zend\Mvc\Controller\AbstractConsoleController;
use Zend\Text\Table;
use Zend\View\Model\ConsoleModel;

class TutorialConsoleController extends AbstractConsoleController
{
    public function showDataAction()
    {
        $table = new Table\Table([
            'columnWidths' => [20, 20] 
        ]);
        $table->setDecorator('ascii');
        $table->appendRow(['FirstName', 'LastName']);
        $table->appendRow(['Abdul Malik', 'Ikhsan']);
        $table->appendRow(['Sharty', 'Mushlihah']);
    
        $consoleModel = new ConsoleModel();
        $consoleModel->setResult($table);
        
        return $consoleModel;
    }
}

Now, you can just register your controller into module.config.php :

return [
    
    'controllers' => [
        'invokables' => [
            'TutorialConsoleModule\Controller\TutorialConsole'
                => 'TutorialConsoleModule\Controller\TutorialConsoleController',
        ],
    ],
    
    'console' => [
        'router' => [
            'routes' => [
                'show-data' => [
                    'options' => [
                        'route'    => 'show data',
                        'defaults' => [
                            'controller' => 'TutorialConsoleModule\Controller\TutorialConsole',
                            'action'     => 'show-data'
                        ]
                    ]
                ]
            ]
        ]
    ],
];

And you can call in console :

php public/index.php show data

and you will get :
console1

Done ūüėČ

Advertisements

3 Responses

Subscribe to comments with RSS.

  1. tot said, on November 29, 2014 at 5:16 pm

    I’ve used console controllers, but I’ve not heard about console view models before. What else can you do with it, beside displaying db data?

    • samsonasik said, on November 29, 2014 at 10:11 pm

      it extends ViewModel, so basically show the data set its errorLevel.

  2. seyferx said, on March 3, 2015 at 11:40 am

    use Zend\Text\Table;
    use Zend\View\Model\ConsoleModel;

    This two were new things for me, thank you!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: