src/Graviton/RestBundle/Listener/LinkHeaderResponseListener.php
private function addSelfLinkHeader($routeName, $routeType, Request $request)
    {
        $routeParams = $request->get('_route_params');
        if (!is_array($routeParams)) {
            $routeParams = [];
        }

        if (($routeType == 'post' || $routeType == 'postNoSlash') || $routeType != 'all') {
            // handle post request by rewriting self link to newly created resource
            $routeParams['id'] = $request->get('id');
        }

        // rewrite post routes to get
        if ($routeType == 'post' || $routeType == 'postNoSlash') {
            $parts = explode('.', $routeName);
            array_pop($parts);
            $parts[] = 'get';
            $routeName = implode('.', $parts);
        }

        $selfLinkUrl = $this->router->generate($routeName, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL);
        $queryString = $request->server->get('QUERY_STRING', '');

        // if no rql was set, we set our default current limits
        if ($request->attributes->get('paging') === true && strpos($queryString, 'limit(') === false) {
            $limit = sprintf(
                'limit(%s,%s)',
                $request->attributes->get('perPage'),
                $request->attributes->get('startAt')
            );

            if (!empty($queryString)) {
                $queryString .= '&';
            }

            $queryString .= $limit;
        }

        if (!empty($queryString)) {
            $selfLinkUrl .= '?' . strtr($queryString, [',' => '%2C']);
        }

        $this->linkHeader->add(
            new LinkHeaderItem(
                $selfLinkUrl,
                ['rel' => 'self']
            )
        );

        return $selfLinkUrl;
    }
private function addSelfLinkHeader($routeName, $routeType, Request $request)
    {
        $routeParams = $request->get('_route_params');
        if (!is_array($routeParams)) {
            $routeParams = [];
        }

        if (($routeType == 'post' || $routeType == 'postNoSlash') || $routeType != 'all') {
            // handle post request by rewriting self link to newly created resource
            $routeParams['id'] = $request->get('id');
        }

        // rewrite post routes to get
        if ($routeType == 'post' || $routeType == 'postNoSlash') {
            $parts = explode('.', $routeName);
            array_pop($parts);
            $parts[] = 'get';
            $routeName = implode('.', $parts);
        }

        $selfLinkUrl = $this->router->generate($routeName, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL);
        $queryString = $request->server->get('QUERY_STRING', '');

        // if no rql was set, we set our default current limits
        if ($request->attributes->get('paging') === true && strpos($queryString, 'limit(') === false) {
            $limit = sprintf(
                'limit(%s,%s)',
                $request->attributes->get('perPage'),
                $request->attributes->get('startAt')
            );

            if (!empty($queryString)) {
                $queryString .= '&';
            }

            $queryString .= $limit;
        }

        if (!empty($queryString)) {
            $selfLinkUrl .= '?' . strtr($queryString, [',' => '%2C']);
        }

        $this->linkHeader->add(
            new LinkHeaderItem(
                $selfLinkUrl,
                ['rel' => 'self']
            )
        );

        return $selfLinkUrl;
    }
private function addSelfLinkHeader ( $ routeName , $ routeType , Request $ request ) { $ routeParams = $ request -> get ( '_route_params' ) ; if ( ! is_array ( $ routeParams ) ) { $ routeParams = [ ] ; } if ( ( $ routeType == 'post' || $ routeType == 'postNoSlash' ) || $ routeType != 'all' ) { // handle post request by rewriting self link to newly created resource $ routeParams [ 'id' ] = $ request -> get ( 'id' ) ; } // rewrite post routes to get if ( $ routeType == 'post' || $ routeType == 'postNoSlash' ) { $ parts = explode ( '.' , $ routeName ) ; array_pop ( $ parts ) ; $ parts [ ] = 'get' ; $ routeName = implode ( '.' , $ parts ) ; } $ selfLinkUrl = $ this -> router -> generate ( $ routeName , $ routeParams , UrlGeneratorInterface :: ABSOLUTE_URL ) ; $ queryString = $ request -> server -> get ( 'QUERY_STRING' , '' ) ; // if no rql was set, we set our default current limits if ( $ request -> attributes -> get ( 'paging' ) === true && strpos ( $ queryString , 'limit(' ) === false ) { $ limit = sprintf ( 'limit(%s,%s)' , $ request -> attributes -> get ( 'perPage' ) , $ request -> attributes -> get ( 'startAt' ) ) ; if ( ! empty ( $ queryString ) ) { $ queryString .= '&' ; } $ queryString .= $ limit ; } if ( ! empty ( $ queryString ) ) { $ selfLinkUrl .= '?' . strtr ( $ queryString , [ ',' => '%2C' ] ) ; } $ this -> linkHeader -> add ( new LinkHeaderItem ( $ selfLinkUrl , [ 'rel' => 'self' ] ) ) ; return $ selfLinkUrl ; }
Add "self" Link header item

@param string  $routeName route name
@param string  $routeType route type
@param Request $request   request

@return string the "self" link url
Add self Link header item