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

Support for content type #13

Open
nbish11 opened this issue Sep 27, 2022 · 0 comments
Open

Support for content type #13

nbish11 opened this issue Sep 27, 2022 · 0 comments

Comments

@nbish11
Copy link

nbish11 commented Sep 27, 2022

What are your thoughts about adding support for content negotiation in the actions?

In the action, using content negotiation methods:

final class GetPhoto
{
    public function html(int $id)
    {
        return new HtmlResponse($this->domain->findById($id));
    }
    public function json(int $id)
    {
        return new JsonResponse($this->domain->findById($id));
    }
}

or using an accepts method:

final class GetPhoto
{
    public function accepts()
    {
        return ['text/html', 'application/json'];
    }
    public function __invoke(int $id)
    {
        // The response is built based off the request format
        return $this->responder->__invoke($this->request, $this->domain->findById($id));
    }
}

The script for both scenarios above:

$matched = $autoRoute->route($request->method, $request->pathInfo, $request->contentType);
$matched->format = /* 'application/json' or 'text/html' */;

// if found for separate content negotiation methods
$matched->method = /* json() or html() */;

// or if found for one single method
$matched->method = /* __invoke() */;

// if not found
$matched->error = /* AutoRoute\Exception\NotAcceptable */;

Personally, I like the first method, as it's simpler to understand and allows more flexibility. However, from my understanding of the ADR pattern, its the responsibility of the Responder to build and return the response as either HTML, JSON, XML, etc. Maybe, the library can support both methods?

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

No branches or pull requests

1 participant