Zend控制台命令不会响应任何内容

时间:2018-12-11 09:41:12

标签: zend-framework console zend-framework3 zend-config

我正在尝试使用zend控制台,并遵循其站点上的文档。这是我的代码。

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",
                    ],
                ],
            ],
        ],
    ],
],

我的控制器

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";

}

当我做php public/index.php abc1时,它什么也不做。什么也没显示。我缺少任何配置吗?

1 个答案:

答案 0 :(得分:0)

我从事的项目所提供的示例:

<?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,
        ],
    ],
];

然后有一个控制器

<?php

namespace MyNameSpace\Console;

use Zend\Mvc\Console\Controller\AbstractConsoleController;

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

以上内容对我来说毫无困难。