Ali Rasheed
Ali Rasheed

Reputation: 2815

Zend console command wouldn't respond anything

I'm trying to use zend console and followed the documentation on their site. This is my code.

module.config.php

"router"                             => [
    "routes"                         => [
        "companies"                  => [
            "type"                   => "segment",
            "options"                => [
                "route"              => "/companies[/:action[/:id]]",
                "constraints"        => [
                    "action"         => "[a-zA-Z][a-zA-Z0-9_-]*",
                    "id"             => "[0-9]*",
                ],
                "defaults"           => [
                    "controller"     => Controller\CompaniesController::class,
                    "action"         => "index",
                ],
            ],
        ],
    ],
],
"console"                            => [
    "router"                         => [
        "routes"                     => [
            "abc1"       => [
                "options"            => [
                    "route"          => "abc1",
                    "defaults"       => [
                        "controller" => Controller\Console::class,
                        "action"     => "abc",
                    ],
                ],
            ],
        ],
    ],
],

My controller

public function abcAction() {
    $request                         =  $this->getRequest();

    if (! $request instanceof ConsoleRequest) {
        throw new RuntimeException("You can only use this action from a console!");
    }

    return "Done! abc.\n";

}

When I do php public/index.php abc1 it does nothing. shows nothing. am I missing any config?

Upvotes: 0

Views: 151

Answers (1)

rkeet
rkeet

Reputation: 3468

Example of what I've got in projects I work on:

<?php

namespace MyNameSpace;

use MyNameSpace\Console\CommandController;
use Zend\Mvc\Console\Router\Simple;

return [
    'console' => [
        'router' => [
            'routes' => [
                'name_of_command'        => [
                    'type'    => Simple::class,
                    'options' => [
                        'route'    => 'name-of-command',
                        'defaults' => [
                            'controller' => CommandController::class,
                            'action'     => 'command',
                        ],
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            // [... ] generated config
            CommandController::class => CommandControllerFactory::class,
        ],
    ],
];

Then got a Controller

<?php

namespace MyNameSpace\Console;

use Zend\Mvc\Console\Controller\AbstractConsoleController;

class CommandController extends AbstractConsoleController
{
    public function commandAction()
    {
        echo 'this is a response';
    }
}

The above works for me in without a hitch.

Upvotes: 0

Related Questions