在Zend Framework 2中,将初始模块横幅添加到控制台应用程序非常简单。
我们需要的是实施getConsoleBanner
和getConsoleUsage
方法并实施Zend\ModuleManager\Feature\ConsoleUsageProviderInterface
或ConsoleBannerProviderInterface
接口。
当通过CLI启动public/index.php
时,这足以在控制台中转储这些消息。
在Zend Framework 3中,它不一样。
执行相同的设置不会提供相同的结果。实际上在控制台中我们看到骨架应用程序的默认html页面与我们通过浏览器访问它的方式相同。
我们在安装自定义模块之前会看到该页面:
以下是zend-mvc-console
模块的文档
https://zendframework.github.io/zend-mvc-console/intro/
即使按照建议安装模块(模块定义中添加了'Zend\Mvc\Console'
),也不会显示控制台横幅。我已经在方法中测试了var dumping并且我能够查看数据,因此框架执行这些方法但在控制台中没有显示结果。
我已经使用控制台路由和控制器进行了测试。找到路径,执行控制器操作,但cli中没有显示任何内容。
我已经挖掘了框架的代码,似乎永远不会执行Zend\Mvc\Console\ResponseSender\ConsoleResponseSender
类。
我是否必须注册一些view_manager
策略才能在CLI中显示某些内容?
以下是zf3骨架应用程序之上的源代码: https://gist.github.com/kachar/06f0c9096bcc1cc0b00f4612aed1b68b
运行应用程序:
$ php -v
PHP 7.0.6 (cli) (built: Apr 27 2016 14:00:40) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
$ php public/index.php
Application\Module::getConsoleBanner
Application\Module::getConsoleUsage
$ php public/index.php user
Application\Controller\IndexController::indexAction
答案 0 :(得分:9)
根据我们自己的文档MVC <-> Console integration is deprecated。我们建议您使用zf-console或symfony console为您的应用程序构建控制台功能。
我们 知道zend-mvc-console的问题,我们将很快发布一个补丁版本来解决它们,这将解决您的短期问题。但是,我们建议长期迁移到另一个解决方案。
答案 1 :(得分:2)
您可以使用zend-mvc-console模块。与zf2控制台相同的API :) 请注意,这个模块很快就会被弃用。
来源:
答案 2 :(得分:1)
对于决定将 zend framework 3(或 laminas)和 symfony/console
一起使用(如@weierophinney 提到的)的任何人,我建议使用来自 zend framework 官方论坛 https://discourse.laminas.dev/t/how-to-launch-a-basic-php-cli/1473/11(作者 rieschl)。我将从那里复制代码到这里:
我写过不同的“单文件”脚本,但最终 如果您的 cli 脚本不断发展,symfony/console 是最好的。它是 非常容易设置。我所做的是让 ServiceManager 创建 Symfony 控制台应用程序,所以我的 CLI 入口点(在 bin/console) 看起来像这样:
#!/usr/bin/env php
<?php
declare(strict_types=1);
use Laminas\Mvc\Application as ZfApp;
use Symfony\Component\Console\Application as ConsoleApp;
chdir(dirname(__DIR__));
require dirname(__DIR__) . '/vendor/autoload.php';
$zfApp = ZfApp::init(require dirname(__DIR__) . '/config/application.config.php');
/** @var ConsoleApp $consoleApp */
$consoleApp = $zfApp->getServiceManager()->get(ConsoleApp::class);
return $consoleApp->run();
<块引用>
如您所见,我构建了 Laminas 应用程序(此处称为 ZfApp,它是 pre-Laminas),从那里获取 ServiceManager 和 Symfony 来自 ServiceManager 的控制台。服务工厂看起来像这样
<?php
declare(strict_types=1);
namespace Eventjet\Factory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
final class ConsoleApplicationFactory
{
public function __invoke(ContainerInterface $container): Application
{
$app = new Application(
'Eventjet Console Legacy System',
'1.0'
);
foreach ($this->createCommands($container) as $command) {
$app->add($command);
}
return $app;
}
/**
* @return Command[]
*/
private function createCommands(ContainerInterface $container): array
{
$commandNames = $this->config($container)['ej-console']['commands'];
return array_map(
static function (string $commandName) use ($container) {
return $container->get($commandName);
},
$commandNames
);
}
/**
* @return mixed[]
*/
private function config(ContainerInterface $container): array
{
return $container->get('config');
}
}
<块引用>
这样,我就可以创建一个新的控制台类,注册为 ['ej-console']['commands'] 配置下配置中的类字符串 键并立即使用它:slight_smile:配置将 看起来像这样:
return [
'ej-console' => [
'commands' => [
SomeCommand::class,
],
],
];
该答案帮助我在一天内开始使用 zf3 和 symfony/console
。