Skip to content
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

Remove the usage of Request::get #7365

Closed
franmomu opened this issue Aug 7, 2021 · 9 comments
Closed

Remove the usage of Request::get #7365

franmomu opened this issue Aug 7, 2021 · 9 comments

Comments

@franmomu
Copy link
Member

franmomu commented Aug 7, 2021

Feature Request

It has been marked as @internal in Symfony 5.4 and its usage is confusing, we should call explicitly the proper input.

@franmomu franmomu changed the title Remove usage of Request::get Remove the usage of Request::get Aug 7, 2021
@VincentLanglet
Copy link
Member

Even if we're only using the query for an input. Changing, for instance,

$request->get('foo')

to

$request->query->get('foo')

is a BC-break since if the user were passing the value in the attributes or the request... So when should we do it ?

Also we are generally setting the data in multiple places. As an example, _sonata_admin is often in the attributes (like when we're on an Admin page), but we sometimes pass it in the query parameter (like when we're calling AppendField).
Should we do something like

$request->attributes->get('_sonata_admin', $request->query->get('_sonata_admin'))

or can we always use the same input ? If so, changing the input can also be considered as BC-break.

@simonberger
Copy link

I would say it can still be valid to ask the attribute, query and request bags without using the Symfony function, but in best case it is placed (early) in a controller where it is known which method is used.

@VincentLanglet
Copy link
Member

The following issue #3062 could help.
Restricting most route for GET will reduce request usage to query and sometimes attributes.

@kirya-dev
Copy link
Contributor

Some optimized:

$request->attributes->get('_sonata_admin') ?: $request->query->get('_sonata_admin')

@VincentLanglet
Copy link
Member

_sonata_admin is used

  • as attribute for lot of routes
  • as query params for some routes like appendFormFieldElement or getShortObjectDescription
  • as request param in RetrieveAutocompleteItems

In the same way, all the data in retrieve autocomplete items action seems to be passed in the request: _context, field, q, ... And I wonder if the $request->query->get($reqParamPageNumber, '1') is not wrong.
Also, it's seems to be the only route where data is passed in the request instead of the query. Even appendFormFieldElement or SetObjectFieldValue or RetrieveFormFieldElement which are made by POST are passing all the data in the query.

In the controller, $request->get($this->admin->getIdParameter()); could be replaced by $request->attribute->get($this->admin->getIdParameter()); and calls like $request->get('btn_preview') could use $request->request instead.

Also, the #7185 issue is showing that sometimes id or parentId are passed as query params instead of attributes so in the AbtractAdmin $this->getRequest()->get($parentAdmin->getIdParameter()) should check the attributes AND the query params.

I find over 60 usage of the get method, and most of them will need at least attributes->get(...) ?? query->get(...) or even attributes->get(...) ?? query->get(...) ?? request->get(...) ; so we won't gain a lot over the usage of this internal method.

Did you plan to make a poc @franmomu ?
Should we really try to stop using the internal method ?
Should we implement a static utils method then which does the same ? I don't think it's great to duplicate the symfony method...

Any thought @jordisala1991 ?

@jordisala1991
Copy link
Member

It would be nice to change all get to what is needed on 4.x, even if it needs a lot of code.

The idea is to reduce that for 5.x if it is possible.

So, maybe now we need to fetch a param from 3 places of the request, but in next major we can refactor the code to only be from one place.

@VincentLanglet
Copy link
Member

It seems to be that we wont be able to reduce the usage of some of them (which arr frequently used) like _sonata_admin or the idParameters like explained in the previous post.

Maybe only half of the get calls can be restricted to the query/request/attributes. But that would require lot of work for small/none gain...

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Copy link

github-actions bot commented Nov 5, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants