0

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

模块.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时,它什么也不做。什么都不显示。我是否缺少任何配置?

4

1 回答 1

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';
    }
}

以上对我来说很顺利。

于 2018-12-11T13:55:15.713 回答