Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Attaching to HAL plugin event manager causes apigility admin to fail to load existing API's #79

Open
pdizz opened this issue Nov 11, 2014 · 5 comments

Comments

@pdizz
Copy link

pdizz commented Nov 11, 2014

After attaching a listener to HAL plugin event manager and trying to edit API's in /apigility/ui#/api,and clicking on an existing API, it wont load. When I comment out the onBootstrap() method in Module.php, the existing API's load normally.

/**
 * Attach the image links listener to the hal render entity event
 *
 * @param MvcEvent $e
 */
public function onBootstrap(MvcEvent $e)
{
    $app = $e->getTarget();
    $services = $app->getServiceManager();
    $helpers  = $services->get('ViewHelperManager');
    $hal      = $helpers->get('Hal');

    $listener = new ImageLinksListener();
    $hal->getEventManager()->attach('renderEntity', function () { return; });
}

The call to /apigility/api/module/FileApi/rest?version=1 fails with this response:

{
    "status":500,
    "title":"Unexpected error",
    "describedBy":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
    "detail":"Missing parameter \"name\"",
    "details":{
        "code":0,
        "message":"Missing parameter \"name\"",
        "trace":"#0 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/Segment.php(313): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, true, true, Array)\n#1 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/Segment.php(409): Zend\\Mvc\\Router\\Http\\Segment->buildPath(Array, Array, false, true, Array)\n#2 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/Part.php(197): Zend\\Mvc\\Router\\Http\\Segment->assemble(Array, Array)\n#3 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/TreeRouteStack.php(330): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#4 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#5 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/TreeRouteStack.php(330): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#6 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/Part.php(210): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#7 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Router\/Http\/TreeRouteStack.php(351): Zend\\Mvc\\Router\\Http\\Part->assemble(Array, Array)\n#8 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/View\/Helper\/Url.php(100): Zend\\Mvc\\Router\\Http\\TreeRouteStack->assemble(Array, Array)\n#9 [internal function]: Zend\\View\\Helper\\Url->__invoke('zf-apigility\/ap...', Array, Array, true)\n#10 \/vagrant_web\/vendor\/zfcampus\/zf-hal\/src\/Plugin\/Hal.php(610): call_user_func(Object(Zend\\View\\Helper\\Url), 'zf-apigility\/ap...', Array, Array, true)\n#11 \/vagrant_web\/vendor\/zfcampus\/zf-hal\/src\/Plugin\/Hal.php(634): ZF\\Hal\\Plugin\\Hal->fromLink(Object(ZF\\Hal\\Link\\Link))\n#12 \/vagrant_web\/vendor\/zfcampus\/zf-hal\/src\/Plugin\/Hal.php(667): ZF\\Hal\\Plugin\\Hal->fromLinkCollection(Object(ZF\\Hal\\Link\\LinkCollection))\n#13 \/vagrant_web\/vendor\/zfcampus\/zf-hal\/src\/Plugin\/Hal.php(440): ZF\\Hal\\Plugin\\Hal->fromResource(Object(ZF\\Hal\\Collection))\n#14 \/vagrant_web\/vendor\/zfcampus\/zf-hal\/src\/View\/HalJsonRenderer.php(123): ZF\\Hal\\Plugin\\Hal->renderCollection(Object(ZF\\Hal\\Collection))\n#15 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/View\/View.php(205): ZF\\Hal\\View\\HalJsonRenderer->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#16 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/View\/Http\/DefaultRenderingStrategy.php(102): Zend\\View\\View->render(Object(ZF\\Hal\\View\\HalJsonModel))\n#17 [internal function]: Zend\\Mvc\\View\\Http\\DefaultRenderingStrategy->render(Object(Zend\\Mvc\\MvcEvent))\n#18 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/EventManager\/EventManager.php(468): call_user_func(Array, Object(Zend\\Mvc\\MvcEvent))\n#19 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/EventManager\/EventManager.php(207): Zend\\EventManager\\EventManager->triggerListeners('render', Object(Zend\\Mvc\\MvcEvent), Array)\n#20 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Application.php(352): Zend\\EventManager\\EventManager->trigger('render', Object(Zend\\Mvc\\MvcEvent))\n#21 \/vagrant_web\/vendor\/zendframework\/zendframework\/library\/Zend\/Mvc\/Application.php(327): Zend\\Mvc\\Application->completeRequest(Object(Zend\\Mvc\\MvcEvent))\n#22 \/vagrant_web\/public\/index_zf2.php(47): Zend\\Mvc\\Application->run()\n#23 {main}"
    }
}
@nebero
Copy link

nebero commented Dec 20, 2014

I also have the same problem.
This is my code(From aligility documentation https://apigility.org/documentation/modules/zf-hal)
public function onBootstrap($e)
{
$app = $e->getTarget();
$services = $app->getServiceManager();
$helpers = $services->get('ViewHelperManager');
$hal = $helpers->get('Hal');
// The HAL plugin's EventManager instance does not compose a SharedEventManager,
// so you must attach directly to it.
$hal->getEventManager()->attach('renderEntity', array($this, 'onRenderEntity'));
}
public function onRenderEntity($e)
{
$entity = $e->getParam('entity');
if (! $entity->entity instanceof SomeTypeIHaveDefined) {
// do nothing
return;
}
// Add a "describedBy" relational link
$entity->getLinks()->add(\ZF\Hal\Link\Link::factory(array(
'rel' => 'describedBy',
'route' => array(
'name' => 'my/api/docs',
),
)));
}

@gaco79
Copy link

gaco79 commented Jan 29, 2015

Same problem here.

Instead of

$app = $e->getTarget();
$services = $app->getServiceManager();
$helpers = $services->get('ViewHelperManager');
$hal = $helpers->get('Hal');
$hal->getEventManager()->attach('renderEntity', array($this, 'onRenderEntity'));

use

$app = $e->getTarget();
$services = $app->getServiceManager();
$sharedEvents = $services->get('SharedEventManager');
$sharedEvents->attach('ZF\Hal\Plugin\Hal', 'renderEntity', array($this, 'onRenderEntity'));

(from https://groups.google.com/a/zend.com/forum/#!msg/apigility-users/p4nm3scFxJM/D9ZjUqECYWUJ)

@ashireman
Copy link

Using the SharedEventManager in the way described by @gaco79 from the Google Groups thread fixed the problem resulting in the error message:

Missing parameter "name"

++ for solution.

@danbroooks
Copy link

+1 for solution

@michalbundyra
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools; a new issue has been opened at laminas-api-tools/api-tools#58.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants