-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added cookbook on using _method in hidden form fields #1603
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
Merged
weaverryan
merged 2 commits into
symfony:2.0
from
richardmiller-zz:adding_method_parameters_cookbook
Jul 30, 2012
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Routing | |
scheme | ||
slash_in_parameter | ||
redirect_in_config | ||
method_parameters | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
.. index:: | ||
single: Routing; _method | ||
|
||
Using HTTP Methods beyond GET and POST in Routes | ||
================================================ | ||
|
||
The HTTP method of a request is one of the requirements that can be checked | ||
when seeing if it matches a route. This is introduced in the routing chapter | ||
of the book ":doc:`/book/routing`" with examples using GET and POST. You | ||
can also use other HTTP verbs in this way. For example, if you have a blog | ||
post entry then you could use the same url pattern to show it, make changes | ||
to it and delete it by matching on GET, PUT and DELETE. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
blog_show: | ||
pattern: /blog/{slug} | ||
defaults: { _controller: AcmeDemoBundle:Blog:show } | ||
requirements: | ||
_method: GET | ||
|
||
blog_update: | ||
pattern: /blog/{slug} | ||
defaults: { _controller: AcmeDemoBundle:Blog:update } | ||
requirements: | ||
_method: PUT | ||
|
||
blog_delete: | ||
pattern: /blog/{slug} | ||
defaults: { _controller: AcmeDemoBundle:Blog:delete } | ||
requirements: | ||
_method: DELETE | ||
|
||
.. code-block:: xml | ||
|
||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<route id="blog_show" pattern="/blog/{slug}"> | ||
<default key="_controller">AcmeDemoBundle:Blog:show</default> | ||
<requirement key="_method">GET</requirement> | ||
</route> | ||
|
||
<route id="blog_update" pattern="/blog/{slug}"> | ||
<default key="_controller">AcmeDemoBundle:Blog:update</default> | ||
<requirement key="_method">PUT</requirement> | ||
</route> | ||
|
||
<route id="blog_delete" pattern="/blog/{slug}"> | ||
<default key="_controller">AcmeDemoBundle:Blog:delete</default> | ||
<requirement key="_method">DELETE</requirement> | ||
</route> | ||
</routes> | ||
|
||
.. code-block:: php | ||
|
||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Route; | ||
|
||
$collection = new RouteCollection(); | ||
$collection->add('blog_show', new Route('/blog/{slug}', array( | ||
'_controller' => 'AcmeDemoBundle:Blog:show', | ||
), array( | ||
'_method' => 'GET', | ||
))); | ||
|
||
$collection->add('blog_update', new Route('/blog/{slug}', array( | ||
'_controller' => 'AcmeDemoBundle:Blog:update', | ||
), array( | ||
'_method' => 'PUT', | ||
))); | ||
|
||
$collection->add('blog_delete', new Route('/blog/{slug}', array( | ||
'_controller' => 'AcmeDemoBundle:Blog:delete', | ||
), array( | ||
'_method' => 'DELETE', | ||
))); | ||
|
||
return $collection; | ||
|
||
Unfortunately, life isn't quite this simple, since most browsers do not | ||
support sending PUT and DELETE requests. Fortunately Symfony2 provides you | ||
with a simple way of working around this limitation. By including a ``_method`` | ||
parameter in the query string or parameters of an HTTP request Symfony2 will | ||
use this as the method when matching routes. This can be done easily in forms | ||
with a hidden field. Suppose you have a form for editing a blog post: | ||
|
||
.. code-block:: html+jinja | ||
|
||
<form action="{{ path('blog_update', {'slug': blog.slug}) }}" method="post"> | ||
<input type="hidden" name="_method" value="PUT" /> | ||
{{ form_widget(form) }} | ||
<input type="submit" value="Update" /> | ||
</form> | ||
|
||
The submitted request will now match the ``blog_update`` route and the ``updateAction`` | ||
will be used to process the form. | ||
|
||
Likewise the delete form could be changed to look like this: | ||
|
||
.. code-block:: html+jinja | ||
|
||
<form action="{{ path('blog_delete', {'slug': blog.slug}) }}" method="post"> | ||
<input type="hidden" name="_method" value="DELETE" /> | ||
{{ form_widget(delete_form) }} | ||
<input type="submit" value="Delete" /> | ||
</form> | ||
|
||
It will then match the ``blog_delete`` route. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We also need the corresponding "maps" entry - in
cookbook/maps.rst.inc