Skip to content

[FrameworkBundle] Add a controller to send simple HTTP responses #30514

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

Closed
wants to merge 1 commit into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Mar 11, 2019

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR todo

This PR adds a new built-in controller, similar to TemplateController and RedirectController, allowing to send simple HTTP response without having to write a custom controller:

# config/routes.yaml
login:
  path: /my-page
  controller: Symfony\Bundle\FrameworkBundle\Controller\SimpleController
  defaults:
    content: 'A simple web page'

This is especially useful when dealing with the security system, here is a fully working example of a JSON-based login endpoint:

# config/routes.yaml
login:
  path: /login
  methods: POST
  controller: Symfony\Bundle\FrameworkBundle\Controller\SimpleController
  defaults:
    content: '{"status": "success"}'
    _format: json
    private: true
# config/packages/security.yaml
security:
    firewalls:
        main:
            anonymous: true
            json_login:
                check_path: /login

It can also be useful for the following use cases:

  • Health check URL for Kubernetes / Nagios / whatever returning a 200 OK and a predefined string
  • Secret URL returning a token proving you're the owner of a domain (Google Analytics validation for instance)

@HeahDude
Copy link
Contributor

Wow! I can add this to #30501 <3, WDYT?

@dunglas
Copy link
Member Author

dunglas commented Mar 11, 2019

@HeahDude while writing the PR description, I was telling myself that the full namespaces where not user friendly! Your PR looks like the missing part, it would be awesome if you add support for this new controller!

@javiereguiluz
Copy link
Member

Two quick questions:

  • Can't you solve this today with a Template controller that renders templates/security/login_success.json.twig and contains {"status": "success"} ?
  • Apart from this JSON login use case, can you imagine any other common use case?

Thanks.

@dunglas
Copy link
Member Author

dunglas commented Mar 11, 2019

@javiereguiluz

Can't you solve this today with a Template controller

It creates a dependency to Twig (not wanted in case of an API), and requires the user to create one extra file.

Apart from this JSON login use case, can you imagine any other common use case?

I think that's a common use case:

  • Health check URL for Kubernetes / Nagios / whatever returning a 200 OK and a predefined string
  • Secret URL returning a token proving you're the owner of a domain (Google Analytics validation for instance)

@javiereguiluz
Copy link
Member

If this is accepted ... I suggest to rename SimpleController as ContentController ... because this is a controller that returns the given content as its response. Also ... the shortcut would be called content, so it'd be consistent.

@HeahDude
Copy link
Contributor

What about RawContentController?

@nicolas-grekas nicolas-grekas added this to the next milestone Mar 11, 2019
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 11, 2019

another one: StaticController :)

@dunglas
Copy link
Member Author

dunglas commented Mar 11, 2019

👍 for StaticController

@lyrixx
Copy link
Member

lyrixx commented Mar 11, 2019

For what it worth, I don't really like it because I like when everything is declared in one place.
With this, there will be some "controllers" in the routing.yml and others in App\Controller\*

That why I even do this for "security controller":

/**
 * @Route("/logout", name="logout")
 */
public function logout()
{
    throw new \BadMethodCallException('The security layer should handle this route.');
}

@dunglas
Copy link
Member Author

dunglas commented Mar 11, 2019

@lyrixx your example doesn't work for json_login.

@lyrixx
Copy link
Member

lyrixx commented Mar 11, 2019

@dunglas This particular example is not really related with your use case. It's just an explanation on why I don't like it.

About your exemple, did you know you can (should?) use route name in the security bundle? instead of check_path: /login you can use check_path: login

@pimolo
Copy link

pimolo commented Mar 11, 2019

I'm not convinced about the DX improvement, it doesn't bring much imo, but a new way to do something we can already do easily and quickly, with some PHP code that every newcomer in Symfony can understand.

@dunglas
Copy link
Member Author

dunglas commented Mar 11, 2019

@lyrixx yes but I've just copied the docs :) Maybe should we update it.

@ktherage
Copy link
Contributor

Hi, i've just a question on it.

Does it is a kind of upgrade of FrameworkBundle:Template:template/Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction?

If it is, IMO this controller (FrameworkBundle:Template:template/Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction) might be renamed to StaticController because they are pretty similar or make it an implementation of the new StaticController.

What do you think about that ?

@fabpot
Copy link
Member

fabpot commented Apr 3, 2019

Not convinced this is that useful.

@chalasr chalasr closed this Apr 3, 2019
@chalasr chalasr reopened this Apr 3, 2019
@chalasr
Copy link
Member

chalasr commented Apr 3, 2019

Sorry for the misclick :)
About the described use case, the right way to set a success response on a json_login listener remains to create an AuthenticationSuccessHandlerInterface implementation and set it as success_handler. Although it looks "simpler", I think it would be confusing to document this feature as such, and could lead to people handling more security stuff in controllers which I recommend to avoid as it means going "outside" of the security system, potentially skipping some important security events or (worse) creating security holes more easily.

@fabpot
Copy link
Member

fabpot commented Jun 4, 2019

Let's close.

@fabpot fabpot closed this Jun 4, 2019
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
nicolas-grekas added a commit that referenced this pull request Feb 9, 2020
…le template and redirect controllers (HeahDude)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[FrameworkBundle][Routing] added Configurators to handle template and redirect controllers

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | let's see
| Fixed tickets | partially #24640, #25145
| License       | MIT
| Doc PR        | symfony/symfony-docs#11120

While working on symfony/symfony-docs#11085, I felt bad about the long notations required for simple [redirects](https://symfony.com/doc/current/routing/redirect_in_config.html) and [templates rendering](https://symfony.com/doc/current/templating/render_without_controller.html) template actions, but I love and use those features since always. Then I gave it a try yesterday night and now I realised I missed #24640 and that #25145 has been closed x).

So here we go, here's my WIP. WDYT of this implementation? ping @javiereguiluz?

I'm going to open the PR in the docs so we can discuss the DX changes there too, and keep focus on the code here.

Cheers!

EDIT
----
This PR now only update PHP-DSL configurators.

______________

TODO:

- [x] gather reviews
- ~[x] fix xml schema~
- [x] add some tests
- ~[ ] handle xsd auto discovery~
- [x] rebase on top of #30507
- [x] ~add shortcuts for #30514~

Commits
-------

de74794 [FrameworkBundle][Routing] added Configurators to handle template and redirect controllers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants