【发布时间】:2018-10-06 23:50:05
【问题描述】:
我想用 zf-console 执行 ZF3 动作。
我可以使用 zend-mvc-console 模块来做到这一点,它工作正常。
例如。
应用程序/config/module.config.php:
'console' => [
'router' => [
'routes' => [
'cronroute' => [
'options' => [
'route' => 'sync',
'defaults' => [
'controller' => Controller\ConsoleController::class,
'action' => 'syncEvents'
]
]
]
]
]
],
Application/src/Controller/ConsoleController.php
class ConsoleController extends AbstractActionController
{
/**
* Entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* User Manager
* @var Application\Service\UserManager
*/
private $userManager;
/**
* Constructor.
*/
public function __construct($entityManager, $userManager)
{
$this->entityManager = $entityManager;
$this->userManager = $userManager;
}
public function syncAction()
{
$response = $this->userManager->syncUserInfo();
return $response ? 'Sync Success' : 'Failed to sync';
}
}
但它说它将被弃用:
https://zendframework.github.io/zend-mvc-console/intro/#deprecated
建议使用 zfcampus 的 zf-console:
https://github.com/zfcampus/zf-console
但我找不到执行控制器操作或使用我的构建服务(如 UserManager)的方法。
有构建 Zend 应用程序和检索服务管理器的示例:
use Zend\Console\Console;
use Zend\Console\ColorInterface as Color;
use ZF\Console\Application;
use ZF\Console\Dispatcher;
chdir(dirname(__DIR__));
require __DIR__ . '/../vendor/autoload.php'; // Composer autoloader
$application = Zend\Mvc\Application::init(require 'config/application.config.php');
$services = $application->getServiceManager();
$buildModel = $services->get('My\BuildModel');
有没有办法用它执行控制器操作?或者我可以加载我的 UserManager 服务吗?
我试图获取 My UserManager:
$buildModel = $services->get('Application\Service\UserManager');
但是接收错误:
PHP Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Unable to resolve service "Application\Service\UserManager" to a factory; are you certain you provided it during configuration?' in /var/www/html/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:687
【问题讨论】:
标签: php zend-framework3