简体   繁体   中英

Plugin for zend framework

$url = $_SERVER['SERVER_NAME'];

    if(!filter_var($url, FILTER_VALIDATE_URL)){
      return false;
    }
    return true;I need to  connect plugin CheckDomain which loaded on pre dispatch with all modules except Admin.

Plugin is a class CheckDomain which could be called as a function CheckDomain() when it's called in that way it checks is domain equal to "test.example.com"

<?php 
namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Mvc\Controller\Plugin\FlashMessenger;
use Zend\Mvc\Controller\Plugin\Forward;
use Zend\Mvc\Controller\Plugin\Layout;
use Zend\Mvc\Controller\Plugin\Params;
use Zend\Mvc\Controller\Plugin\PostRedirectGet;
use Zend\Mvc\Controller\Plugin\Redirect;
use Zend\Mvc\Controller\Plugin\Url;
use Zend\View\Model\ViewModel;

 class CheckDomainPlugin extends AbstractPlugin{

  public function checkdomain()
  {

        $url = $_SERVER['SERVER_NAME'];

        if(!filter_var($url, FILTER_VALIDATE_URL)){
            return false;
       }
        return true;
    }  
}

I call it for every controller except Admin, but I need to use it once.

I mean is it possible to load automatically plugin for all modules axcept admin

if you wish to "hook up" into every module you should read the zf2 documentation about MVC Events and the EventManager class.

http://framework.zend.com/manual/2.3/en/modules/zend.mvc.mvc-event.html#the-mvcevent

http://framework.zend.com/manual/2.3/en/modules/zend.event-manager.event-manager.html

Here is a small example for your Application/Module.php

public function onBootstrap(MvcEvent $e)
{
    $application =  $e->getApplication();
    $serviceManager = $application->getServiceManager();
    $eventManager = $application->getEventManager();
    $sharedManager = $eventManager->getSharedManager();

    // DISPATCH EVENT
    $sharedManager->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function( MvcEvent $e) use ($serviceManager) {

        $controller      = $e->getTarget();
        $controllerClass = get_class($controller);
        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));

        // this is the first segment from the module namespace
        // if the Admin Namespace is something like this Admin/Controller/...

        if( $moduleNamespace != 'Admin' ) {

            $CheckDomainPlugin = $serviceManager->get('ControllerPluginManager')->get('CheckDomainPlugin');
            // do something
        }
    }, 50 );
}

If you create a plugin, or any service, they will be available to all modules within the application. If there are areas in which the plugin should not be used then don't call it!

If I actually understand your problem; I would use an event listener for this. If you listen 'on dispatch' you can exclude all the admin controllers if you give them a unique interface.

 // Module.php
 public function onBootstrap($event)
 {
   $application = $event->getApplication();
   $eventManager = $application->getEventManager()->getSharedManager();

   $eventManager->attach(
     'Zend\Mvc\Controller\AbstractActionController', 
     'dispatch', 
     function($e) {
       $target = $e->getTarget(); // The dispatched controller

       if ($controller instanceof AdminControllerInterface) {
         return;
       }
       // Do something here
     }
   );
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM