Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to override itemOperations.get #1411

Open
wow-apps opened this issue Feb 10, 2020 · 1 comment
Open

Question: how to override itemOperations.get #1411

wow-apps opened this issue Feb 10, 2020 · 1 comment
Labels

Comments

@wow-apps
Copy link

Can someone help me to override GET method with default path.

What I need:
GET /api/content_pages/{id} should use my custom controller, where I need to return result using header Accept-Language

// config/api/content_page.yaml
resources:
  Api\Entity\ContentPage:
    itemOperations:
      get:
        route_name: 'get_content_page'
        methods: ['GET']
        normalization_context:
          groups: ['v1_get_item']
        swagger_context:
          parameters:
            -
              name: id
              in: path
              required: true
              type: string
              description: "Content page identifier"
// config/routes.yaml
get_content_page:
  path: /content_pages/{id}
  methods: ['GET']
  condition: "request.headers.get('Accept-Language') matches '/^[a-z]{2}-[A-Z]{2}$/i'"
  defaults:
    _controller: Api\Controller\ContentPagesController::getContentPage
    _api_resource_class: Api\Entity\ContentPage
    _api_item_operation_name: get
?php
// src/Controller/ContentPagesController.php
namespace Api\Controller;
use Api\Entity\ContentPage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class ContentPagesController extends AbstractController
{
    /**
     * @param Request $request
     * @param string  $id
     * @return ContentPage
     */
    public function getContentPage(Request $request, string $id): ContentPage
    {
        dd($request->headers->get('Accept-Language'));
    }
}

I'm getting No route found for \"GET \/content_pages\/about\": Method Not Allowed (Allow: PUT, DELETE)

If, I do next change:

// config/api/content_page.yaml
resources:
  Api\Entity\ContentPage:
    itemOperations:
      get: ~
      get_item:
        route_name: 'get_content_page'
        methods: ['GET']
        normalization_context:
          groups: ['v1_get_item']
        swagger_context:
          parameters:
            -
              name: id
              in: path
              required: true
              type: string
              description: "Content page identifier"

I get Invalid identifier value or configuration.

@wow-apps
Copy link
Author

Problem was in condition: "request.headers.get('Accept-Language') matches '/^[a-z]{2}-[A-Z]{2}$/i'" .... trying to figure out solution...

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

No branches or pull requests

2 participants