cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Update Request.zep

Open zikezhang opened this issue 2 years ago • 3 comments

The Http request service cannot invoke filterService from container.

Hello!

  • Type: bug fix
  • Link to issue: based on the documentation there: https://docs.phalcon.io/5.0/en/request#preset-sanitizers I implemented sanitizers on my Http request service with the following code:
public function register(DiInterface $di) : void
    {
        $di->setShared($this->serviceName, function () {
            $request = new Request();
            $request->setParameterFilters(
                        'id',[\Phalcon\Filter\Filter::FILTER_ABSINT],['post']
                    )
                    ->setParameterFilters(
                        'title',
                        [
                            \Phalcon\Filter\Filter::FILTER_TRIM,
                            \Phalcon\Filter\Filter::FILTER_STRIPTAGS
                        ],
                        ['post']
                    )
                    ->setParameterFilters(
                        'email',
                        [
                            \Phalcon\Filter\Filter::FILTER_EMAIL,
                            \Phalcon\Filter\Filter::FILTER_TRIM
                        ],
                        ['post']
                    )
            ;
            return $request;
        });
    }

And then, i got an exception message, which means the DI container is null

"A dependency injection container is required to access the 'filter' service"

in this case, I have updated getFilterService() so that the Request Service can invoke system Filter properly.

In raising this pull request, I confirm the following:

  • [x] I have read and understood the Contributing Guidelines
  • [x] I have checked that another pull request for this purpose does not exist
  • [x] I wrote some tests for this PR

Thanks Zike

zikezhang avatar Nov 10 '23 16:11 zikezhang

@zikezhang Are you using Di or FactoryDefault for your container?

niden avatar Nov 11 '23 14:11 niden

Hi @niden , I am using Phalcon\Di\FactoryDefault for the Http container.

zikezhang avatar Nov 12 '23 01:11 zikezhang

I am wondering if we can rename private filterService = null; to private filter = null; like it's in the Cookie component.

zikezhang avatar Nov 15 '23 00:11 zikezhang