Skip to content

Deprecated method Request::get() in symfony/http-foundation 7.4 #834

@jonasschen

Description

@jonasschen

It's ocurring a lot of warning messages in my projects like this:

WARNING: Since symfony/http-foundation 7.4:  is deprecated, use properties ->attributes, query or request directly instead. in /srv/vendor/symfony/deprecation-contracts/function.php on line 25

This error occur exactly in the following files:

prettus/l5-repository/src/Prettus/Repository/Criteria/RequestCriteria.php:(41 - 48)

// ... 

38:    public function apply($model, RepositoryInterface $repository)
39:    {
40:        $fieldsSearchable = $repository->getFieldsSearchable();
41:        $search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
42:        $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null);
43:        $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null);
44:        $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
45:        $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
46:        $with = $this->request->get(config('repository.criteria.params.with', 'with'), null);
47:        $withCount = $this->request->get(config('repository.criteria.params.withCount', 'withCount'), null);
48:        $searchJoin = $this->request->get(config('repository.criteria.params.searchJoin', 'searchJoin'), null);
        // ...

prettus/l5-repository/src/Prettus/Repository/Traits/CacheableRepository.php:75

// ... 

69:    public function isSkippedCache()
70:    {
71:        $skipped = isset($this->cacheSkip) ? $this->cacheSkip : false;
72:        $request = app('Illuminate\Http\Request');
73:        $skipCacheParam = config('repository.cache.params.skipCache', 'skipCache');
74:
75:        if ($request->has($skipCacheParam) && $request->get($skipCacheParam)) {
76:            $skipped = true;
77:        }
        // ...

prettus/l5-repository/src/Prettus/Repository/Presenter/FractalPresenter.php:81

// ... 

74:    protected function parseIncludes()
75:    {
76:
77:        $request = app('Illuminate\Http\Request');
78:        $paramIncludes = config('repository.fractal.params.include', 'include');
79:
80:        if ($request->has($paramIncludes)) {
81:            $this->fractal->parseIncludes($request->get($paramIncludes));
82:        }
        // ...

Error reason

The Request class includes a get() method that retrieves a parameter by checking, in order, the route attributes, the GET parameters, and the POST parameters. As soon as it finds a match, it returns the value and stops searching:

// ...
use Symfony\Component\HttpFoundation\Request;

class SomeController extends AbstractController
{
    public function someAction(Request $request): Response
    {
        // the value can come from the route, the query string, or POST data
        $value = $request->get('some_key');

        // ...
    }
}

However, Symfony has long recommended accessing these values through the attributes, query or request properties directly. In Symfony 7.4, the Request::get() method is deprecated. Starting in Symfony 8.0, it will no longer be available:

public function someAction(Request $request): Response
{
    // use this if the value comes from route placeholders or custom attributes
    $value = $request->attributes->get('some_key');

    // use this for GET query parameters
    $value = $request->query->get('some_key');

    // use this for POST-submitted data
    $value = $request->request->get('some_key');

    // ...
}

Learn more: https://symfony.com/blog/new-in-symfony-7-4-request-class-improvements#deprecated-the-get-method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions