Skip to content

Point that route parameters are also Request attributes #6219

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 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cookbook/routing/extra_information.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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:
arguments to your controller, and as attributes of the ``Request`` object:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We change this a bit and only tell the reader that we can access the parameters in the controller:

[...] that will then be accessible in your controller::


.. configuration-block::

Expand Down Expand Up @@ -52,12 +52,16 @@ arguments to your controller:

return $collection;

Now, you can access this extra parameter in your controller::
Now, you can access this extra parameter in your controller, either as an argument (if specified), or through the ``Request`` object::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the addition here and change the code to the example proposed by @javiereguiluz.


public function indexAction($page, $title)
use Symfony\Component\HttpFoundation\Request;

public function indexAction(Request $request, $page, $title)
{
// ...
$titleAttribute = $request->attributes->get('title'); // same as $title
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$titleAttribute is confusing imo, to show this, its enough to write $request->attributes->get('title'); // same as $title

what do you thing @javiereguiluz

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. We could even split the example in two parts:

// Getting the title via controller arguments

public function indexAction($page, $title)
{
    // ...
}

// Getting the title via request attributes

use Symfony\Component\HttpFoundation\Request;

public function indexAction(Request $request)
{
    $title = $request->attributes->get('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.
but you can still access its value from inside your controller, or from the ``Request`` object's ``attributes`` bag.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please break the line here after the first word that crosses the 72nd character?