我一直在尝试在 Onbootstrap 函数中获取自定义服务实例。我没有成功。
请查看我的代码,我收到以下错误
Fatal error: Uncaught Laminas\ServiceManager\Exception\ServiceNotFoundException: Unable to resolve service "Application\MongoConfiguration" to a factory; are you certain you provided it during configuration?"
模块.php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Doctrine\Common\Cache\Cache;
use Application\MongoConfiguration;
class Module
{
const VERSION = '3.0.2dev';
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$sm = $app->getServiceManager();
$Config = $sm->get(\Application\MongoConfiguration::class)->getMongoConfig();
print_r($Config);exit;
}
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}
模块.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/v1/application[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
],
],
'service_manager' => [
'factories' => [
Application\MongoConfiguration::class => Application\Factory\MongoConfigurationFactory::class
]
],
'controller_plugins' => [
'factories' => [
Controller\Plugin\Commonhelper::class => Controller\Plugin\Factory\CommonhelperFactory::class,
],
'aliases' => [
'common' => Controller\Plugin\Commonhelper::class,
],
'invokables' => array(
'apierror' => 'Application\Controller\Plugin\Errorhelper'
)
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
'strategies' => array(
'ViewJsonStrategy',
),
],
'doctrine' => [
'driver' => [
'odm_default' => [
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../src/MongoEntity']
],
'drivers' => [
__NAMESPACE__ . '\MongoEntity' => __NAMESPACE__ . '_driver'
]
]
]
],
];
工厂\MongoConfigurationFactory.php
namespace Application\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\MongoConfiguration;
class MongoConfigurationFactory implements FactoryInterface
{
private $config;
private $serviceLocator;
public function createService(ServiceLocatorInterface $serviceLocator)
{
return $servicelocator->get(MongoConfiguration::class);
}
}
MongoConfiguration.php
namespace Application;
class MongoConfiguration
{
private $mongoConfig;
public function getMongoConfig(){
return 'Hi';
}
}
我想让 MongoConfiguration 位于 onBootstrap 函数中的 Module.php 本身中。
你能告诉我我错过了什么吗?