-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Update HTTP method requirement example #6062
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -815,10 +815,10 @@ Adding HTTP Method Requirements | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
In addition to the URL, you can also match on the *method* of the incoming | ||
request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you have a contact form | ||
with two controllers - one for displaying the form (on a GET request) and one | ||
for processing the form when it's submitted (on a POST request). This can | ||
be accomplished with the following route configuration: | ||
request (i.e. GET, HEAD, POST, PUT, DELETE). Suppose you create an API for | ||
your blog and you have 2 routes: One for displaying a post (on a GET or HEAD | ||
request) and one for updating a post (on a PUT request). This can be | ||
accomplished with the following route configuration: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -830,39 +830,39 @@ be accomplished with the following route configuration: | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
// ... | ||
|
||
class MainController extends Controller | ||
class BlogApiController extends Controller | ||
{ | ||
/** | ||
* @Route("/news") | ||
* @Method("GET") | ||
* @Route("/api/posts/{id}") | ||
* @Method({"GET","HEAD"}) | ||
*/ | ||
public function newsAction() | ||
public function showAction($id) | ||
{ | ||
// ... display your news | ||
// ... return a JSON response with the post | ||
} | ||
|
||
/** | ||
* @Route("/contact") | ||
* @Method({"GET", "POST"}) | ||
* @Route("/api/posts/{id}") | ||
* @Method("PUT") | ||
*/ | ||
public function contactFormAction() | ||
public function editAction($id) | ||
{ | ||
// ... display and process a contact form | ||
// ... edit a post | ||
} | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/routing.yml | ||
news: | ||
path: /news | ||
defaults: { _controller: AppBundle:Main:news } | ||
methods: [GET] | ||
api_show_post: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just asking: should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, that makes sense - so there would be a loose standard of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
path: /api/posts/{id} | ||
defaults: { _controller: AppBundle:BlogApi:show } | ||
methods: [GET, HEAD] | ||
|
||
contact_form: | ||
path: /contact | ||
defaults: { _controller: AppBundle:Main:contactForm } | ||
methods: [GET, POST] | ||
api_edit_post: | ||
path: /api/posts/{id} | ||
defaults: { _controller: AppBundle:BlogApi:edit } | ||
methods: [PUT] | ||
|
||
.. code-block:: xml | ||
|
||
|
@@ -873,12 +873,12 @@ be accomplished with the following route configuration: | |
xsi:schemaLocation="http://symfony.com/schema/routing | ||
http://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<route id="news" path="/news" methods="GET"> | ||
<default key="_controller">AppBundle:Main:news</default> | ||
<route id="api_show_post" path="/api/posts/{id}" methods="GET|HEAD"> | ||
<default key="_controller">AppBundle:BlogApi:show</default> | ||
</route> | ||
|
||
<route id="contact_form" path="/contact" methods="GET|POST"> | ||
<default key="_controller">AppBundle:Main:contactForm</default> | ||
<route id="api_edit_post" path="/api/posts/{id}" methods="PUT"> | ||
<default key="_controller">AppBundle:BlogApi:edit</default> | ||
</route> | ||
</routes> | ||
|
||
|
@@ -889,24 +889,25 @@ be accomplished with the following route configuration: | |
use Symfony\Component\Routing\Route; | ||
|
||
$collection = new RouteCollection(); | ||
$collection->add('news', new Route('/news', array( | ||
'_controller' => 'AppBundle:Main:contact', | ||
), array(), array(), '', array(), array('GET'))); | ||
$collection->add('api_show_post', new Route('/api/posts/{id}', array( | ||
'_controller' => 'AppBundle:BlogApi:show', | ||
), array(), array(), '', array(), array('GET', 'HEAD'))); | ||
|
||
$collection->add('contact_form', new Route('/contact', array( | ||
'_controller' => 'AppBundle:Main:contactForm', | ||
), array(), array(), '', array(), array('GET', 'POST'))); | ||
$collection->add('api_edit_post', new Route('/api/posts/{id}', array( | ||
'_controller' => 'AppBundle:BlogApi:edit', | ||
), array(), array(), '', array(), array('PUT'))); | ||
|
||
return $collection; | ||
|
||
.. versionadded:: 2.2 | ||
The ``methods`` option was introduced in Symfony 2.2. Use the ``_method`` | ||
requirement in older versions. | ||
|
||
Despite the fact that these two routes have identical paths (``/contact``), | ||
the first route will match only GET requests and the second route will match | ||
only POST requests. This means that you can display the form and submit the | ||
form via the same URL, while using distinct controllers for the two actions. | ||
Despite the fact that these two routes have identical paths | ||
(``/api/posts/{id}``), the first route will match only GET or HEAD requests and | ||
the second route will match only PUT requests. This means that you can display | ||
and edit the post with the same URL, while using distinct controllers for the | ||
two actions. | ||
|
||
.. note:: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dislike posts here, this is unnecessary ambiguous. (post vs posts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a common practice is to have consistent URLs for your API (to avoid the inconsistency stuff like github's pulls and issues urls). Also, without it the example wouldn't make any sense anymore.