0

我的路线定义:

'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,
                        ],

                    ],
                ],

            ],

        ],
    ],

数据数据地图定义:

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

生成结果:

{
"_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": [.....] }}

所有链接都是用不完整的href生成的,只有域部分,路由部分被剥离..

预期的结果是这样的:

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

我做错了,我不知道从哪里开始..

谢谢大家给我一些想法

简化控制器代码:

$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:我没有使用完整的zend-expressive只是zend-framework ..

4

1 回答 1

1

抱歉,我忘记了我上周做过的事情:(

UrlGeneratorInterface 必须做一些自定义实现才能成功地将 zend-expressive-hal 集成到 zend 框架(原始类 ExpressiveUrlGenerator 使用 Expressive\Helper\ServerUrlHelper & UrlHelper,Expressive 的一部分)

所以我使用 Zend\View\Helper\ServerUrl & Url 来完成它。

我的代码中有小错字。最后一节课在这里:

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);
    }
}

我希望代码可以帮助某人。

于 2018-11-16T18:20:20.917 回答