-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Clarify that route defaults don't need a placeholder #4017
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
.. index:: | ||
single: Routing; Extra Information | ||
|
||
How to Pass Extra Information from a Route to a Controller | ||
========================================================== | ||
|
||
Parameters inside the ``defaults`` collection don't necessarily have to | ||
match a placeholder in the route ``path``. In fact, you can use the | ||
``defaults`` array to specify extra parameters that will then be accessible as | ||
arguments to your controller: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
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. I think you should add the filename as a comment inside the code block:: # app/config/routing.yml |
||
# app/config/routing.yml | ||
blog: | ||
path: /blog/{page} | ||
defaults: | ||
_controller: AcmeBlogBundle:Blog:index | ||
page: 1 | ||
title: "Hello world!" | ||
|
||
.. code-block:: xml | ||
|
||
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. same here: <!-- app/config/routing.xml --> |
||
<!-- app/config/routing.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" path="/blog/{page}"> | ||
<default key="_controller">AcmeBlogBundle:Blog:index</default> | ||
<default key="page">1</default> | ||
<default key="title">Hello world!</default> | ||
</route> | ||
</routes> | ||
|
||
.. code-block:: php | ||
|
||
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. and here: // app/config/routing.php |
||
// app/config/routing.php | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Route; | ||
|
||
$collection = new RouteCollection(); | ||
$collection->add('blog', new Route('/blog/{page}', array( | ||
'_controller' => 'AcmeBlogBundle:Blog:index', | ||
'page' => 1, | ||
'title' => 'Hello world!', | ||
))); | ||
|
||
return $collection; | ||
|
||
Now, you can access this extra parameter in your controller:: | ||
|
||
public function indexAction($page, $title) | ||
{ | ||
// ... | ||
} | ||
|
||
As you can see, the ``$title`` variable was never defined inside the route path, | ||
but you can still access its value from inside your controller. | ||
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. [...] route path, but you [...] 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. @xabbuh told me to remove this comma. Should I add it back? 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. 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. Yes, that's right. I'll add it back. 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. 👍 Sorry for the confusion. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ Routing | |
service_container_parameters | ||
custom_route_loader | ||
redirect_trailing_slash | ||
extra_information |
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 think "extra" is a closed-class word and show be lowercased.
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.
@weaverryan can you confirm this?
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.
extra is an adjective, and adjectives are mostly (or always) considered open class words. So I think this should be upper-cased.
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 see, I think I then did some lowercasing wrong in the past. Maybe I should do a re-check. :)