ZF2 Get ServiceManager from Controller Plugin Factory - php

I'm trying to create a new Controller Plugin using a factory to inject a dependency.
public function createService(ServiceLocatorInterface $serviceLocator) {
$services = $serviceLocator->getServiceLocator();
/** #var \Zend\Mvc\Controller\PluginManager */
$plugin = new MyPlugin();
if ($services->has('my_service')) {
$plugin->setService($services->get('my_service'));
}
return $plugin;
}
The problem is $services can't find 'my_service'
I've added the proper configurations in my service manager
'services' => array(
'invokables' => array(
'my_service' => 'Application\Service\MyService'
)
),
'controller_plugins' => array(
'factories' => array(
'my_plugin' => 'Application\Controller\Plugin\Factory\MyPlugin'
)
)
My thinking is it's a bug in the PluginManager where it isn't injecting the service manager properly.

I've added the proper configurations in my service manager
The key for service manager configuration is service_manager, not services which is why your invokable is not found, change the key ...
'service_manager' => array(
'invokables' => array(
'my_service' => 'Application\Service\MyService'
)
),
// ...

Related

Plugins not working in controller

I'm building my first Zend Framework 2 application with the skeleton tutorial. But whenever I try to call any plugin from any controller, I get the error message:
A plugin by the name "PLUGIN_NAME" was not found in the plugin manager
Zend\Mvc\Controller\PluginManager
Unfortunately I don't know which part of the code could help you to help me. I post some files which I think could be important.
config/modules.config
return [
'Zend\Router',
'Zend\Validator',
'Zend\Form',
'Album',
'Application',
'User',
];
module/User/src/Module.php
<?php
namespace User;
use User\Model\User;
use User\Model\UserTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
const VERSION = '1.0.0dev';
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'User\Model\UserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
module/User/config/module.config.php
<?php
namespace User;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Router\Http\Segment;
return [
'controllers' => [
'factories' => [
Controller\UserController::class => InvokableFactory::class,
],
'invokables' => [
'User\Controller\User' => 'User\Controller\UserController',
],
],
'router' => [
'routes' => [
'user' => [
'type' => Segment::class,
'options' => [
'route' => '/user[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\UserController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'user' => __DIR__ . '/../view',
],
],
];
I'm trying to call my plugins like this from a controller action:
$this->flashMessanger()->...
$this->identity();
$this->getServiceLocator();
Because I really needed the Service Locator, I found a workaround, which is not so nice I think, but works for me:
$sm = $this->getEvent()->getApplication()->getServiceManager();
But I guess something is wrong here.
Edit:
For a better reproduction of what I did (I installed Zend Framework again and it still gives me the same error):
Installed Zend Framework (2.4 I guess?) by this installation guide (https://framework.zend.com/manual/2.4/en/ref/installation.html). I installed it with the following command:
composer create-project -sdev --repository-url="https://packages.zendframework.com" zendframework/skeleton-application
On the installation, when I was asked if I want to install a minimum install, I chose "No". Every next question I answered with "Yes" (the installer asks to install a lot of modules, I installed them all).
The installer asked in which config I want to inject "ZendDeveloperTools". I answered 2 (config/development.config.php.dist). For all others I chose 1 (config/modules.config.php).
I tested the skeleton application by calling the url in my browser, it worked.
I downloaded the Album module from GitHub here (https://github.com/Hounddog/Album).
I copied the album module to my modules folder.
I added the entry 'Album' to my config/modules.config.php file
I browsed to the album page
I'm getting the error:
A plugin by the name "getServiceLocator" was not found in the plugin
manager Zend\Mvc\Controller\PluginManager
I assume the reason for this error is the getAlbumTable() method in the AlbumController
public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('Album\Model\AlbumTable');
}
return $this->albumTable;
}
In the ZF2 docs for controller plugins you can read that your controller has to implement the following methods: setPluginManager, getPluginManager and plugin. You can also read:
For an extra layer of convenience, both AbstractActionController and AbstractActionController have __call() implementations that allow you to retrieve plugins via method calls:
$plugin = $this->url();
Does your controller extend AbstractActionController or AbstractActionController? If yes, it should work as mentioned in the docs (so those methods you mention in your question should work).
Since you didn't share any controller code it is hard to say whether this is the problem...
UPDATE
The error you get is not related to the configuration of your ControllerPluginManager, but you get this error because you are doing:
$sm = $this->getServiceLocator();
Since the method getServiceLocator doesn't exist the magic __call() method is executed and this leads to the error.
This is because in the latest versions of ZF2 the controller classes are no longer 'service locator aware' meaning you cannot retrieve the ServiceManager by calling $this->getServiceLocator().
Instead you will have to inject your Album\Model\AlbumTable service into the controller class inside a factory:
1) Add a constructor method to your controller class:
public function __construct(AlbumTable $albumTable){
$this->albumTable = $albumTable;
}
2) Create a factory for your controller:
<?php
namespace Album\Controller\Factory;
use Album\Controller\AlbumController;
class AlbumControllerFactory implements FactoryInterface
{
/**
* #param ServiceLocatorInterface $serviceLocator
* #return AlbumController
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$controllerPluginManager = $serviceLocator;
$serviceManager = $controllerPluginManager->get('ServiceManager');
$albumTable = $serviceManager->get('Album\Model\AlbumTable');
return new AlbumController($albumTable);
}
}
3) Register your controller factory inside your module.config.php:
'factories' => [
`Album\Controller\AlbumController` => `Album\Controller\Factory\AlbumControllerFactory`,
],
The function getAlbumTable() was used in ZF2 tutorial but in ZF3 it was replaced by member table.
Change the deleteAction() function in AlbumController.php to read:
public function deleteAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'No');
if ($del == 'Yes') {
$id = (int) $request->getPost('id');
$this->table->deleteAlbum($id);
}
// Redirect to list of albums
return $this->redirect()->toRoute('album');
}
return array(
'id' => $id,
'album' => $this->table->getAlbum($id)
);
}
Note the two lines reading $this->table->... instead of $this->getAlbumTable()->...

Different uses of ServiceLocatorInterface in ZF2 Application

I have 2 factories.
The first is a Controller Factory:
<?php
namespace Blog\Factory;
use Blog\Controller\ListController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ListControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');
return new ListController($postService);
}
}
The second is a Post ServiceFactory:
<?php
namespace Blog\Factory;
use Blog\Service\PostService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class PostServiceFactory implements FactoryInterface
{
/**
* Create service
*
* #param ServiceLocatorInterface $serviceLocator
* #return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new PostService(
$serviceLocator->get('Blog\Mapper\PostMapperInterface')
);
}
}
Here is my module config:
<?php
return array(
'service_manager' => array(
'factories' => array(
'Blog\Service\PostServiceInterface' => 'Blog\Factory\PostServiceFactory'
)
),
'controllers' => array(
'factories' => array(
'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
)
),
'router' => array(
// Open configuration for all possible routes
'routes' => array(
// Define a new route called "post"
'post' => array(
// Define the routes type to be "Zend\Mvc\Router\Http\Literal", which is basically just a string
'type' => 'literal',
// Configure the route itself
'options' => array(
// Listen to "/blog" as uri
'route' => '/blog',
// Define default controller and action to be called when this route is matched
'defaults' => array(
'controller' => 'Blog\Controller\List',
'action' => 'index',
)
)
)
)
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
)
);
In the controller factory, I have to call getServiceLocator against the ServiceLocatorInterface, followed by the get call. however in the post service factory i just call get. I did a dump and it looks like both are the Zend\ServiceManager\ServiceManager classes. When I tried performing the getServiceLocator call against the post service factory service locator it errored no method found.
Im not quite understanding whats going on?
Controller factories are called in a different way than casual service factories. The ServiceLocator passed to createService is actually not the ServiceManager you are looking for but an instance of ControllerManager.
If you try this:
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');
var_dump(get_class($serviceLocator));
return new ListController(postService );
}
you'll get the output:
string(37) "Zend\Mvc\Controller\ControllerManager"
while the same dump in your PostServiceFactory will give you:
string(34) "Zend\ServiceManager\ServiceManager"
From the Zend 2 documentation:
http://framework.zend.com/manual/current/en/in-depth-guide/services-and-servicemanager.html#writing-a-factory-class
When using a Factory-Class that will be called from the ControllerManager it will actually inject itself as the $serviceLocator. However, we need the real ServiceManager to get to our Service-Classes. This is why we call the function getServiceLocator() who will give us the real ServiceManager.

PHP-DI is not working in integration with ZendFramework2

Could you please give a hint why PHP-DI integration with Zend Framework 2 is not working for me (reproduced with Apache/2.4.9 (Win64) PHP/5.5.12 and Apache/2.2.22 (Win32) PHP/5.3.13).
composer.json:
{
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.3.5",
"mnapoli/php-di": "4.4.6",
"mnapoli/php-di-zf2": "0.3.0",
...
},
...
config\application.config.php:
<?php
return array(
'modules' => array(
'Morpho',
'DI\ZendFramework2',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
),
);
?>
module/Morpho/config.module.config.php:
<?php
return array(
'service_manager' => array(
'factories' => array(
'DI\Container' => function() {
$builder = new DI\ContainerBuilder();
$builder->addDefinitionsFromFile("config/di.yml");
return $builder->build();
},
),
),
'router' => array(
...
),
'controllers' => array(
...
),
'view_manager' => array(
...
),
);
config/di.yml:
Morpho\Service\PartOfSpeechService:
class: Morpho\Service\PhpMorphyPartOfSpeechService
module/Morpho/src/Morpho/Controller/PartOfSpeechController:
class PartOfSpeechController extends AbstractRestfulController {
...
/**
* #Inject
* #var PartOfSpeechService
*/
public $partOfSpeechService;
public function processPostData(Request $request) {
$partsOfSpeech = $this->partOfSpeechService->getPartsOfSpeech("test", "en_EN");
return new JsonModel($partsOfSpeech);
}
}
When running this code under apache each time I get:
PHP Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException'
with message 'Module (DI\ZendFramework2) could not be initialized.' in \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php:195
Stack trace:
0 \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(169): Zend\ModuleManager\ModuleManager->loadModuleByName(Object(Zend\ModuleManager\ModuleEvent))
1 \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(96): Zend\ModuleManager\ModuleManager->loadModule('DI\ZendFramewor...')
2 [internal function]: Zend\ModuleManager\ModuleManager->onLoadModules(Object(Zend\ModuleManager\ModuleEvent))
3 \vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468):
call_user_func(Array, Object(Zend\ModuleManager\ModuleEvent))
4 \vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventM in \vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php on line 195
Any your thoughts would be really appreciated.
It doesn't work because you are using the old YAML syntax, but since PHP-DI v4.0 the syntax is now PHP.
Head over to the documentation to learn about the syntax: http://php-di.org/doc/definition.html
For a service:
factory config:
'factories' => array(
'MyService' => 'Application\Factory\MyService',
),
Factory class:
class MyService implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceManager)
{
$purifier = new MyService($serviceManager->get('MyAwesomeDependency'));
return $purifier;
}
}
For a controller:
ControllerFactory.php:
class PartOfSpeechControllerFactory
{
public function __invoke($serviceLocator)
{
// Service locator here is the ControllerManager so get ServiceManager
$serviceManager = $serviceLocator->getServiceLocator();
$controller = new PartOfSpeechController($serviceManager->get('PartOfSpeechService'));
return $controller;
}
}
class PartOfSpeechController.php
class PartOfSpeechController extends AbstractRestfulController {
protected $partOfSpeechService;
public function __construct(PartOfSpeechService $partOfSpeechService)
{
$this->partOfSpeechService = $partOfSpeechService;
}
public function processPostData(Request $request) {
$var = $this->partOfSpeechService->serviceMethod();
}
}
The config for controller:
'factories' => array(
'Application\Controller\PartOfSpeechController' => 'Application\Factory\PartOfSpeechControllerFactory'
),
I followed the suggestion given by Purple Hexagon and here is a working implementation using Service Manager:
module/Morpho/config:
...
'service_manager' => array(
'services' => array(
"PartOfSpeechService" => new Morpho\Service\PhpMorphyPartOfSpeechService(),
),
),
...
module/Morpho/src/Morpho/Controller/PartOfSpeechController.php:
class PartOfSpeechController extends AbstractRestfulController {
...
public function processPostData(Request $request) {
$serviceManager = $this->getServiceLocator();
$partsOfSpeech = $serviceManager->get("PartOfSpeechService")->getPartsOfSpeech($request->getPost("phrase"),
$request->getPost("language"));
return new JsonModel($partsOfSpeech);
}
}
Why I don't like this:
I have to use a "dummy" code to obtain serviceManager. That's "dummy" because that's not related to business logic of my application at all.
The Dependency Injection approach provided by ServiceManager makes my code dependent on the ServiceManager itself. Typically I should not care how a bean/object is injected and so shouldn't refer to any kind of container or ServiceManager in my code.
I think PHP-DI is much more closer to the bean injection model used by Java Spring (that I believe is good). Unfortunately it is still not working for me.
And finally, the approach of obtaining object from container was working in PHP-DI as well.

Module getServiceConfig not injecting TableGateway into repository

I can't seem to get my table gateway to inject into my repository(service)...
I have the following:
Module.php:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\Concrete\AlbumRepository' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumRepository($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('albums', $dbAdapter, null, $resultSetPrototype);
}
)
);
}
and here is my module.config.php:
return array(
'service_manager' => array(
'invokables' => array(
'Album\Model\Abstracts\IAlbumRepository' => 'Album\Model\Concrete\AlbumRepository'
),
),
'controllers' => array(
'factories' => array(
'Album\Controller\Album' => 'Album\Model\Factories\AlbumControllerFactory',
),
),
the error is:
Catchable fatal error: Argument 1 passed to Album\Model\Concrete\AlbumRepository::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, none given, called in C:\xampp\htdocs\ZendFrameworkTest\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 1035 and defined in C:\xampp\htdocs\ZendFrameworkTest\module\Album\src\Album\Model\Concrete\AlbumRepository.php on line 11
Note:
the delegate functions on the factories array are just not getting called, i'm doing somethign silly but i can't tell what.
I'm also doing dependancy injection, i'm guessing this is where it's going wrong as the factory is creating the repository object without the injection:
class AlbumControllerFactory implements FactoryInterface
{
/**
* Create service
*
* #param ServiceLocatorInterface $serviceLocator
*
* #return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Need to do something here?
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Album\Model\Abstracts\IAlbumRepository');
return new AlbumController($postService);
}
}
You defined your repository as invokable, that means that service manager tries to create it by instantiating directly without any params.
change it to alias
return array(
'service_manager' => array(
'aliases' => array(
'Album\Model\Abstracts\IAlbumRepository' => 'Album\Model\Concrete\AlbumRepository'
),
),
);

zf2 Trying to inject input filter into service

I'm attempting to create a custom filter and inject it into a service via factory.
use Zend\InputFilter\InputFilter;
class WSRequestFilter extends InputFilter{
protected $inputFilter;
public function init(){
$this->add( array(
'name' => 'apiVersion',
'required' => true,
'filters' => [
array('name' => 'Real'),
...
In Module.php...
public function getServiceConfig(){
return array(
...
'factories' => array(
'Puma\Service\WebServiceLayer' => function($sm) {
$wsRequestFilter = new Filter\WSRequestFilter();
$wsRequestFilter->init();
$wsl = new Service\WebServiceLayer($wsRequestFilter);
return $wsl;
},
),
);
}
But I get service not found exception when executing $wsRequestFilter->init();. I have also tried to initialize the filter using the InputFilterManager similar to here but I got a service not found trying to access the manager via $serviceManager->get('InputFilterManager'). I think I am missing something fundamental here.
The init() method invoked automatically by InputFilterManager just after the filter object created. You don't need to invoke manually.
Add this to your module configuration:
'input_filters' => array(
'invokables' => array(
'ws-request-filter' => '\YourModule\Filter\WSRequestFilter',
),
),
And change your service factory like below:
public function getServiceConfig(){
return array(
...
'factories' => array(
'Puma\Service\WebServiceLayer' => function($sm) {
$filter = $sm->get('InputfilterManager')->get('ws-request-filter')
$wsl = new \YourModule\Service\WebServiceLayer($filter);
return $wsl;
},
),
);
}
It should work.

Categories