Last active
July 4, 2016 11:14
-
-
Save smozgur/226e3dc9c508c2a4eef61e96a9386e9d to your computer and use it in GitHub Desktop.
ZF3 - Solved - Failure to get Controller Plugin worked globally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* config/global.php to keep application settings. | |
*/ | |
return [ | |
/* ... */ | |
'application' => [ | |
'settings' => [ | |
'secretkey' => '84bew58h37a07t50e09v79eb98fre843', | |
'pagination' => [ | |
'itemsPerPage' => 13, | |
], | |
], | |
], | |
/* ... */ | |
?> | |
<?php | |
/** | |
* Application Plugin Factory | |
* To create Application Plugin by injecting config array | |
*/ | |
namespace Application\Controller\Plugin\Factory; | |
use Zend\ServiceManager\Factory\FactoryInterface; | |
use Interop\Container\ContainerInterface; | |
use Application\Controller\Plugin\Application; | |
class ApplicationFactory implements FactoryInterface | |
{ | |
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | |
{ | |
$config = $container->get('config'); | |
$settings = $config['application']['settings']; | |
$plugin = new Application($settings); | |
return $plugin; | |
} | |
} | |
?> | |
<?php | |
/** | |
* Application Plugin | |
* Actual plugin to provide config array in the controllers | |
*/ | |
namespace Application\Controller\Plugin; | |
use Zend\Mvc\Controller\Plugin\AbstractPlugin; | |
use Zend\Config\Config; | |
class Application extends AbstractPlugin | |
{ | |
protected $settings; | |
public function __construct($settings) | |
{ | |
$this->settings = new Config($settings); | |
} | |
public function getSettings() | |
{ | |
return $this->settings; | |
} | |
} | |
?> | |
<?php | |
/** | |
* module.config.php | |
* Application Module Configuration file | |
*/ | |
namespace Application; | |
return [ | |
/* ... */ | |
'controller_plugins' => [ | |
'aliases' => [ | |
'application' => Controller\Plugin\Application::class, | |
], | |
'factories' => [ | |
Controller\Plugin\Application::class => Controller\Plugin\Factory\ApplicationFactory::class, | |
], | |
], | |
/* ... */ | |
]; | |
<?php | |
/** | |
* Following works as expected in Application IndexController action | |
* but fails in any other controller in the entire project with | |
* “A plugin by the name “application” was not found in the plugin manager Zend\Mvc\Controller\PluginManager” | |
* More interesting: I can see the "application" alias is loaded in the zend-developer-tools config bar | |
*/ | |
print $this->application()->getSettings()->pagination->itemsPerPage; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resolution: I found out that plugins are not registered before controller __construct. I was trying to keep settings in a local variable by assigning it in the controller's __construct.
I will just use the actual plugin in controller actions instead.