Incomplete links generated in hal (zend-expressive-hal + zend-router)

135 views Asked by At

My routes def:

'router' => [
        'routes' => [
            'TimeTable' => [
                'type' => 'Literal',
                'options' => [
                    // Change this to something specific to your module
                    'route' => '/tt',

                ],
                'may_terminate' => false,
                'child_routes' => [


                'API' => [
                    'type' => 'Literal',
                    'options' => [
                        // Change this to something specific to your module
                        'route' => '/api',

                    ],
                    'may_terminate' => false,
                    'child_routes' => [
                        'lines' => [
                            'type'    => 'Literal',
                            'options' => [
                                // Change this to something specific to your module
                                'route'    => '/lines',
                                'defaults' => [
                                    'controller' => Controller\LineRestApiController::class,
                                ],
                            ],
                            'may_terminate' => true,
                        ],

                    ],
                ],

            ],

        ],
    ],

Medatada map def:

MetadataMap::class => [
         [
            '__class__' => RouteBasedCollectionMetadata::class,
            'collection_class' => LineCollection::class,
            'collection_relation' => 'lines',
            'route' => 'TimeTable/API/Lines',
        ],
]

Generated result:

{
"_total_items": 78,
"_page": 1,
"_page_count": 4,
"_links": {
"self": {
"href": "http://xxx.xxx.xx"
},
"next": {
"href": "http://xxx.xxx.xx"
},
"last": {
"href": "http://xxx.xxx.xx"
}
},
"_embedded": {
"lines": [.....] }}

All links are generated with incomplete href , there is only domain part, route part is stripped ..

Expected result is something like this:

"href" : "http://xxx.xxx.xx/xxx/tt/api/lines....."

Im doing something wrong, I have no idea where to start ..

Thanks everyone for giving me some ideas

Simplified Controller code:

$psr7request = Psr7ServerRequest::fromZend($this->getRequest()); 

$list = this->entityManager->getRepository(Line::class)->getValidLinesCollection();

$resource = $this->resourceGenerator->fromObject($list, $psr7request); 

echo Psr7Response::toZend($this->responseFactory->createResponse($psr7request, $resource))->getBody();

exit;

PS: Im not using full zend-expressive just zend-framework..

1

There are 1 answers

0
neradp On

Sorry i forgot that i had done in last week :(

It must be done some custom implementation for UrlGeneratorInterface to successful integrate zend-expressive-hal to zend framework (original class ExpressiveUrlGenerator uses Expressive\Helper\ServerUrlHelper & UrlHelper, the part of Expressive)

So i used Zend\View\Helper\ServerUrl & Url to done it.

I have small typo in code. The final class is here:

use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Hal\LinkGenerator\UrlGeneratorInterface;
use Zend\View\Helper\ServerUrl as ServerUrlHelper;
use Zend\View\Helper\Url as UrlHelper;

class HalUrlGenerator implements UrlGeneratorInterface
{
    /**
     * @var null|ServerUrlHelper
     */
    private $serverUrlHelper;

    /**
     * @var UrlHelper
     */
    private $urlHelper;

    public function __construct(UrlHelper $urlHelper, ServerUrlHelper $serverUrlHelper = null)
    {
        $this->urlHelper = $urlHelper;
        $this->serverUrlHelper = $serverUrlHelper;
    }

    public function generate(
        ServerRequestInterface $request,
        string $routeName,
        array $routeParams = [],
        array $queryParams = []
    ) : string {

        $urlHelper = $this->urlHelper;
        $path = $urlHelper($routeName, $routeParams, ['query'=> $queryParams]);

        if (! $this->serverUrlHelper) {
            return $path;
        }

        $serverUrlHelper = $this->serverUrlHelper;



        return $serverUrlHelper($path);
    }
}

I hope the code can help somebody.