code run before every actions in a module ZF2? - zend-framework2

I want to write some code to run before every actions in my module. I have tried hooking onto onBootstrap() but the code run on the other modules too.
Any suggestions for me?

There are two ways to do this.
One way is to create a serice and call it in every controllers dispatch method
Use onDispatch method in controller.
class IndexController extends AbstractActionController {
/**
*
* #param \Zend\Mvc\MvcEvent $e
* #return type
*/
public function onDispatch(MvcEvent $e) {
//Call your service here
return parent::onDispatch($e);
}
public function indexAction() {
return new ViewModel();
}
}
don't forget to include following library on top of your code
use Zend\Mvc\MvcEvent;
Second method is to do this via Module.php using event on dispatch
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module {
public function onBootstrap(MvcEvent $e) {
$sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', array($this, 'addViewVariables'), 201);
}
public function addViewVariables(Event $e) {
//your code goes here
}
// rest of the Module methods goes here...
//...
//...
}
How to create simple service using ZF2
reference2
reference3

Related

How to replace class with mock in DI? Phalcon + Codeception

I try writing functional tests for my controllers using Codeception testing framework. I want to replace real service in DI with fake one.
Controller code example:
<?php
namespace App\Controllers;
class IndexController extends ControllerBase
{
public function indexAction()
{
// some logic here
$service = $this->getDI()->get('myService');
$service->doSomething();
// some logic here
}
}
Test code example:
<?php
namespace App\Functional;
class IndexControllerCest
{
public function testIndexAction(FunctionalTester $I)
{
// Here i want to mock myService, replace real object that in controller with fake one
$I->amOnRoute('index.route');
}
}
I already try different combinations with Codeception Phalcon module like addServiceToContainer.
I setup Codeception using bootstrap.php file almost the same as for real app.
Phalcon version: 3.4.1
Codeception version: 3.1
So my question in last code fragment on comment section. Thank you for any help.
I would like suggest you start from creating a separated helpers to create and inject dependencies as follows:
# functional.suite.yml
class_name: FunctionalTester
modules:
enabled:
- Helper\MyService
- Phalcon:
part: services
# path to the bootstrap
bootstrap: 'app/config/bootstrap.php'
# Another modules ...
Create a separated service:
<?php
namespace Helper;
use Codeception\Module;
/** #var \Codeception\Module\Phalcon */
protected $phalcon;
class MyService extends Module
{
public function _initialize()
{
$this->phalcon = $this->getModule('Phalcon');
}
public function haveMyServiceInDi()
{
$this->phalcon->addServiceToContainer(
'myService',
['className' => '\My\Awesome\Service']
);
}
}
And use it in tests as follows:
<?php
namespace App\Functional;
use Helper\MyService;
class IndexControllerCest
{
/** #var MyService */
protected $myService;
protected function _inject(MyService $myService)
{
$this->myService = $myService;
}
public function testIndexAction(FunctionalTester $I)
{
$I->wantTo(
'mock myService, replace real object that in controller with fake one'
);
$this->myService->haveMyServiceInDi();
$I->amOnRoute('index.route');
}
}

ZF3 - EventManager and dispatch event

in an older ZF2 application I change the layout in a dispatch listener, if the matched route starts with admin. Now I started a new project and want to use ZF3 components, but the event manager does have some changes and I get the following exception:
Uncaught TypeError: Argument 2 passed to Zend\EventManager\EventManager::attach() must be callable, none given
I don't know really how to handle this in ZF3. Here are my relevant source codes to change the layout in my ZF2 application:
Module.php
namespace Admin;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
class Module implements BootstrapListenerInterface {
public function onBootstrap(EventInterface $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$eventManager->attach($serviceManager->get('Admin\Listener\Dispatch'));
}
}
DispatchListener.php
namespace Admin\Listener;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
class DispatchListener extends AbstractListenerAggregate {
public function attach(EventManagerInterface $eventManager) {
$this->listeners[] = $eventManager->attach(
MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'), 100
);
}
public function onDispatch(EventInterface $event) {
$matchedRouteName = $event->getRouteMatch()->getMatchedRouteName();
if (strpos($matchedRouteName, 'admin') === 0) {
$event->getViewModel()->setTemplate('layout/admin');
}
}
}
zf3 is more focused on decoupling components, it seems aggregates has been removed to attach event see the api document
event manager
for short the attach message says
attach($eventName, callable $listener, $priority = 1) : callable
I hope since you are not specifying the eventName you are getting the error message
update:
see the link to migration guide from v2 to v3 for event manager
Removed functions
In ZF3 you can change your layout for your controller this easy way:
<?php
namespace YourCompanyModule;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\MvcEvent;
class Module
{
// The "init" method is called on application start-up and
// allows to register an event listener.
public function init(ModuleManager $manager)
{
// Get event manager.
$eventManager = $manager->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method.
$sharedEventManager->attach(__NAMESPACE__, 'dispatch',
[$this, 'onDispatch'], 100);
}
// Event listener method.
public function onDispatch(MvcEvent $event)
{
// Get controller to which the HTTP request was dispatched.
$controller = $event->getTarget();
// Get fully qualified class name of the controller.
$controllerClass = get_class($controller);
// Get module name of the controller.
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
// Switch layout only for controllers belonging to our module.
if ($moduleNamespace == __NAMESPACE__) {
$viewModel = $event->getViewModel();
$viewModel->setTemplate('layout/layout2');
}
}
// ...
}

How to inject interfaces (not class) in symfony3?

I have a problem with Symfony DependencyInjection Component. I want to inject interfaces into controllers, so I could only use interface methods. But, I notice I can use any public method from class that implement the interface and this is wrong. I follow the great article: http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/
Write the test service class and interface
interface ITestService
{
public function interfaceFunction();
}
class TestService implements ITestService
{
public function interfaceFunction() {/* do somenthing */}
public function classFunction() {/*do somenthing*/}
}
Configure my application service class as a service (test_service)
# file: app/config/services.yml
test_service:
class: MyApp\Application\Services\TestService
Configure my controller as a service:
# file: app/config/services.yml
test_controller:
class: MyApp\AppBundle\Controller\TestController
arguments:
- '#test_service'
Using service in controller
class TestController extends Controller
{
private testService;
function _construct(ITestService $testService)
{
$this->testService = $testService;
}
public function indexAction()
{
// This should be inaccesible but it works :(
$this->testService->classFunction();
// This is the only function I should use.
$this->testService->interfaceFunction();
}
As #Timurib says, this is because despite having Type Hintings, PHP doesn't evaluate the methods to call until runtime. This could be seen as something undesirable, but it allows to use some technics such as Duck Typing.
Here you have a simplified example based on the one you're providing (it doesn't put the Symfony Container into the mix, because this is something purely related to PHP). You can run it on 3v4l.org:
interface IService
{
public function interfaceFunction();
}
final class ServiceWithOtherFunction implements IService
{
public function interfaceFunction() { echo "ServiceWithOtherFunction interfaceFunction\n"; }
public function otherFunction() { echo "ServiceWithOtherFunction otherFunction\n"; }
}
final class Controller
{
private $service;
public function __construct(IService $service)
{
$this->service = $service;
}
public function indexAction()
{
$this->service->interfaceFunction();
$this->service->otherFunction();
}
}
$controllerWithOtherFunction = new Controller(new ServiceWithOtherFunction);
$controllerWithOtherFunction->indexAction();
Output:
ServiceWithOtherFunction interfaceFunction
ServiceWithOtherFunction otherFunction
But when we inject another implementation that does not contains the otherFunction, the code throws an Error at runtime:
final class ServiceWithoutOtherFunction implements IService
{
public function interfaceFunction() { echo "ServiceWithoutOtherFunction interfaceFunction\n"; }
}
$controllerWithoutOtherFunction = new Controller(new ServiceWithoutOtherFunction);
$controllerWithoutOtherFunction->indexAction();
Output:
ServiceWithoutOtherFunction interfaceFunction
Fatal error: Uncaught Error: Call to undefined method ServiceWithoutOtherFunction::otherFunction() in /in/mZcRq:28
Stack trace:
#0 /in/mZcRq(43): Controller->indexAction()
#1 {main}
thrown in /in/mZcRq on line 28
Process exited with code 255.
If you're going towards the use of interfaces, DI, and DIC, you should not call any public method rather than the exposed by the interface. This is the only way to really take advantadge of the benefits of having an interface: Decoupling from the implementation details, and be able to change the class to be injected without changing anything inside your Controller.

Zend Framework 2 - How to give declare folder path in terms of "use" and "namespace"

I am facing problem while creating adapter object in controller file named Listcontroller.My code is
namespace Blog\Controller;
use Blog\Service\PostServiceInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Sql\Sql;
use Zend\Db\Adapter\Adapter;
class ListController extends AbstractActionController
{
/**
* #var \Blog\Service\PostServiceInterface
*/
protected $postService;
public function __construct(PostServiceInterface $postService)
{
$this->postService = $postService;
}
public function indexAction()
{
$adapter = new Zend\Db\Adapter\Adapter($configArray);
print_r($adapter);
//code ....
}
}
here it is serching to find Zend\Db\Adapter\Adapter inside Blog\Controller.
Error is -> Fatal error: Class 'Blog\Controller\Zend\Db\Adapter\Adapter' not found. Can anybody tell me please how can i move two folder back from above path. so that i can get proper object??
You don't need the fully qualified class name (FQCN) when instantiating a new Adapter since you declare that class path via use statement:
use Zend\Db\Adapter\Adapter;
Change this block
public function indexAction()
{
$adapter = new Zend\Db\Adapter\Adapter($configArray);
print_r($adapter);
}
to
public function indexAction()
{
$adapter = new Adapter($configArray);
print_r($adapter);
}
It should work.
Anyway, new \Zend\Db\Adapter\Adapter($configArray) also works (notice the first backslash) but its longer, harder to type and less readable than first example.
You may also want to read namespace aliasing/importing section of the documentation.

ZendFramework 2 - how to call zfcUser from the Model class

I am trying to call ZFCUser from a class within my model but i keep getting this message:
Call to undefined method Members\Model\MemberTable::zfcUserAuthentication()
this is how i tried to call it:
public function getUserEntity()
{
if($this->zfcUserAuthentication()->getAuthService()->hasIdentity())
{
if (!$this->user_entity)
{
$this->setUserEntity($this->zfcUserAuthentication()->getAuthService()->getIdentity());
}
return $this->user_entity;
}
}
i suspect that i need to implement/extend a class inorder for ZFcuser to be recoginised.
i would really appriciate some quick advice on this.
thank you
by the way
the ZFCUser works when i call it from my controller so, the use Zend\Mvc\Controller\AbstractActionController obviouly enables this to be recoginised.
but what is the equivalent of the AbstractActionController for other classes in your framework.
zfcUserAuthentication() is a controller plugin, so that's why it can be called from controllers. You can't (or shouldn't) try and access this from non-controllers. If you need the user entity within another class, you should pass it in as a dependency for that class.
Edit: Update your member table factory so it sets the user entity:
'Members\Model\MemberTable' => function($sm) {
$tableGateway = $sm->get('MemberTableGateway');
$table = new MemberTable($tableGateway);
$authService = $serviceLocator->get('zfcuser_auth_service');
$userEntity = $authService->getIdentity();
$table->setUserEntity($userEntity);
return $table;
}
add a property to your MemberTable class for it:
protected $userEntity;
and getters/setter for it:
public function setUserEntity($userEntity)
{
$this->userEntity = $userEntity;
}
public function getUserEntity()
{
return $this->userEntity;
}
then just call $this->getUserEntity() when you need it.
There are various ways to do that -
One of the way is - do the following in "MemberTable.php" file.
Maybe most of the below lines of code is already available in our project.
a. Add the below lines after 'namespace' statement -
use Zend\ServiceManager\ServiceLocatorAwareInterface; //Added Line
use Zend\ServiceManager\ServiceLocatorInterface; //Added Line
b. Change the class statement as -
class MemberTable implements ServiceLocatorAwareInterface { //Modified Line
....
.....
}
c. Add the below line at the top of the class statement -
class MemberTable implements ServiceLocatorAwareInterface {
protected $serviceLocator; //Added line
....
.....
}
d. Add the following functions -
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
e. In Members/Module.php, make the changes,
'Members\Model\MemberTable' => function($sm) {
$tableGateway = $sm->get('MemberTableGateway');
$table = new MemberTable($tableGateway);
$table->setServiceLocator($sm); //This is the important line.
return $table;
}
Now you have the 'ServiceLocator' available in the MemberTable class.
For accessing the zfcUserAuthentication() or any ControllerPlugin, you can do the following -
public function getUserEntity()
{
$zfcUserAuth = $this->getServiceLocator()->get('controllerPluginManager')->get('zfcUserAuthentication');
if($zfcUserAuth->getAuthService()->hasIdentity())
{
if (!$this->user_entity)
{
$this->setUserEntity($zfcUserAuth->getAuthService()->getIdentity());
}
return $this->user_entity;
}
}

Resources