Integration of (new!) zend-console within MVC - does ist actually work?

I understand zend-mvc-console is deprecated. Usage of zend-console is encouraged. According to the official current docs MVC-integration is still possible. So I followed the docs and just added console config im my module.config.php:

return [
    'router' => [
        'routes' => [
            // HTTP routes are defined here
        ],
    ],

    'console' => [
        'router' => [
            'routes' => [
               'foo' => [
                    'options' => [
                        'route'    => 'foo',
                        'defaults' => [
                            'controller' => Foo\Controller\Index::class,
                            'action'     => 'index',
                        ],
                    ],
                ],
            ],
        ],
    ],

    /* ... */
];

However: Whatever I enter in the terminal ($ php public/index.php foo or just $ php public/index.php, $ php7.2 public/index.php doesn’t help either) it doesn’t matter index,php just always prints out HTML-Code into my terminal as if I would enter http://localhost/fooapp/public/ (with successful Code 200) in my browser.

I’m really getting desperate, why doesn’t it work? What’s missing? Glad for any helpful hint or link.
Thanks
m.

How does our index controller look like? Does your indexAction method include a ViewModel?
Similar to this?

public function indexAction()
{
    return new ViewModel();
}

If yes, that’s why you see html.

Maybe try something along the line of

public function indexAction()
{
    return "I am a CLI call";
}

Thanks. I tried that.

But the problem seems to be a different one. According to the docs on zend-console with MVC, runing $ php public/index.php without any arguments or routes on a terminal should simply output the current Zend Framework version plus a failure reason:

I’ve got the impression ZF3 simply ignores my console config variables at module.config.php. For testing purposes I defined non existent controllers in the console config array, like this:

'console' => [
        'router' => [
            'routes' => [
                'default-route' => [
                    'type'     => 'catchall',
                    'options' => [
                        'route'    => '',
                        'defaults' => [
                            'controller' => 'xApplication\Controller\Index',
                            'action'     => 'nonExistentAction',
                        ],
                    ],
                ],
                
                
                'foo' => [
                    'options' => [
                        'route'    => 'foo bar',
                        'defaults' => [
                            'controller' => xxApplication\Controller\Index::class,
                            'action'     => 'nonExistentAction',
                        ],
                    ],
                ],
            ],
        ],
    ],

Neither does ZF print out a failure reason as supposed nor is a fatal error triggered since I specified non existent controllers or actions.

Any ideas anybody?:sob:

I know that’s not the answer you are looking for, but this github comment from MWOP is all I can dig up for you.


We have the following components related to console functionality:

  • zendframework/zend-console provides an adapter-based approach for working with the console at a low-level.
  • zendframework/zend-mvc-console provides bindings for using zend-mvc controllers to provide handlers for console purposes, using zendframework/zend-console.
  • zfcampus/zf-console provides a console microframework for writing console application using zendframework/zend-console. (zfcampus is the organization where Apigility, our API builder and runtime, lives; zf-console was developed there as we had need for easier console tooling within that project.)

I hope that clears up where each of these packages lives and the purposes each serves.

Regardless, each of these is considered deprecated . Maintaining them requires too much effort, particularly when there are other solutions in this problem space.

Thanks! This actually did help! :grinning: Once I installed zend-mvc-console in addition to the already installed zend-console it just worked fine!
Unfortunately the docs on zend-console don’t mention at all that you need to install zend-mvc-console in order to benefit fom MVC-Support for zend-console. Also, there is no hint about the deprecation thing. It would have saved me a lot of hassle if this would’ve been clearer in the official documentation! :face_with_raised_eyebrow:

Is there a way to suggest a correction of the documentation to Zend?

You are welcome to do so. The documentation can be found on GitHub. Clone it, make the changes and create a PR for it :slight_smile:

1 Like

See and add your comments: