Ich versuche Service Locator/Entity Manager in Plugin-Klasse zu bekommen, wie kann ich das bekommen.ZF2 getServiceLocator in ControllerPlugin-Klasse
In meinem Controller bekomme ich es so.
aber in Plugin-Klasse bekomme ich Fehler auf $ this-> getServiceLocator() Zeile. weil dies in der Plugin-Klasse nicht verfügbar ist.
Wie kann ich das gleiche tun, so dass ich einige Datensätze abrufen und einige in der Datenbank im Plugin einfügen kann.
Ich habe MvcEvent $ e Objekt in meiner Plugin-Klasse, kann ich das nutzen, um Entity Manager zu bekommen?
habe ich this plugin verwenden meine Plugin
Jede Führungs appriciated wird zu erstellen.
Update:
namespace Auth\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\EventManager\EventInterface as Event;
use Zend\Authentication\AuthenticationService;
use Doctrine\ORM\EntityManager;
use Auth\Entity\User;
use Zend\Mvc\MvcEvent;
class AclPlugin extends AbstractPlugin
{
/*
* @var Doctrine\ORM\EntityManager
*/
protected $em;
public function checkAcl($e)
{
$auth = new AuthenticationService();
if ($auth->hasIdentity()) {
$storage = $auth->getStorage()->read();
if (!empty($storage->role))
$role = strtolower ($storage->role);
else
$role = "guest";
} else {
$role = "guest";
}
$app = $e->getParam('application');
$acl = new \Auth\Acl\AclRules();
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
$action = $matches->getParam('action', 'index');
$resource = strtolower($controller);
$permission = strtolower($action);
if (!$acl->hasResource($resource)) {
throw new \Exception('Resource ' . $resource . ' not defined');
}
if ($acl->isAllowed($role, $resource, $permission)) {
$query = $this->getEntityManager($e)->createQuery('SELECT u FROM Auth\Entity\User u');
$resultIdentities = $query->execute();
var_dump($resultIdentities);
exit();
return;
} else {
$matches->setParam('controller', 'Auth\Controller\User'); // redirect
$matches->setParam('action', 'accessdenied');
return;
}
}
public function getEntityManager($e) {
var_dump($this->getController()); // returns null
exit();
if (null === $this->em) {
$this->em = $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
public function setEntityManager(EntityManager $em) {
$this->em = $em;
}
}
ich oben Klasse in module.php
public function onBootstrap(Event $e)
{
$application = $e->getApplication();
$services = $application->getServiceManager();
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration'),101);
}
public function loadConfiguration(MvcEvent $e)
{
$e->getApplication()->getServiceManager()
->get('ControllerPluginManager')->get('AclPlugin')
->checkAcl($e); //pass to the plugin...
}
nenne
Ich melde mich dieses Plugin in module.config.php
return array(
'controllers' => array(
'invokables' => array(
'Auth\Controller\User' => 'Auth\Controller\UserController',
),
),
'controller_plugins' => array(
'invokables' => array(
'AclPlugin' => 'Auth\Controller\Plugin\AclPlugin',
),
),
);
Plugin-Klasse ist unter Controller \ Plugin \ und aufrufbaren durch Rückkehr Array ('controller_plugins' => array ( 'invokables' => array ( 'AclPlugin' => 'Auth \ Controller \ Plugin \ AclPlugin', ), ),); aber irgendwie bekomme ich einen Fehler, wenn ich $ this-> getController() -> getServiceLocator() -> get ('doctrine.entitymanager.orm_default') verwende; in plugin class error ist Fatal error: Aufruf einer Member-Funktion getServiceLocator() auf einem Nicht-Objekt in /module/Auth/src/Auth/Controller/Plugin/AclPlugin.php – Developer
Kannst du bitte deine Controller-Plugin-Klasse einfügen? Ich mache das genauso und es funktioniert für mich. Bitte geben Sie auch Ihre ZF2-Version (betaN/RCn/2.0.0) an. –
Version ist zf2 stabile relase 2.0.0. Bitte sehen Sie sich die betreffende Klasse an. – Developer