How to create resourceGenerator - Expressive HAL

expressive
zend-expressive-hal

#1

Hi,

I try to use zend-expressive-hal like in the documentation (https://docs.zendframework.com/zend-expressive-hal/intro/) but something is not clear for me.

$resourceGenerator, $responseFactory, … are injected in the Action middleware.

class BookAction implements ServerMiddlewareInterface
{

public function __construct(
    Repository $repository,
    ResourceGenerator $resourceGenerator,
    HalResponseFactory $responseFactory
) {
    $this->repository = $repository;
    $this->resourceGenerator = $resourceGenerator;
    $this->responseFactory = $responseFactory;
}

...

But how can i generate the $resourceGenerator and $responseFactory in my Action middleware factory ?? With which value? Is it the right place?

class BookFactory
{
public function __invoke(ContainerInterface $container)
{
$repository = $container->get(BookTable::class);

    $responseFactory = ???
    $resourceGenerator = ???

    return new BookAction($repository, $responseFactory, $resourceGenerator);
}

}

I already create a MetadataMap::class in the config.

Someone can help me?


#2

I answer to my self… :wink:

In Action Factory retrieve class like that:

    $responseFactory = $container->get(HalResponseFactory::class);
    $resourceGenerator = $container->get(ResourceGenerator::class);

And add in configProvider:

public function getDependencies()
{
    return [

        'factories'  => [
            ...
            Zend\Expressive\Hal\ResourceGenerator::class => Zend\Expressive\Hal\ResourceGeneratorFactory::class,
            Zend\Expressive\Hal\HalResponseFactory::class => Zend\Expressive\Hal\HalResponseFactoryFactory::class
            ...
        ],
    ];
}

#3

If you are using Expressive, and have the zend-component-installer plugin as a development dependency (it’s enabled by default in the skeleton), you should get prompted when adding zend-expressive-hal to your application to add it to your config/config.php file; if you selected “yes”, these factories will be registered for you already.

If you didn’t select “yes”, then you can add the zend-expressive-hal ConfigProvider manually to your config/config.php file:

$aggregator = new ConfigAggregator([
    // Add it here:
    \Zend\Expressive\Hal\ConfigProvider::class,

#4

Thanks @matthew, our posts have crossed. I found my problem. I had to register 2 factories (HAL) in my config, like in my second post.