We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Error message when using integer filter in getQuery function of Http\Request object

Hi @ll!

I like to use the built in filter in the getQuery function of Http\Request object:

$request = new \Phalcon\Http\Request();
echo ("->".$request->getQuery('id', 'int', 0));

Unfortunately this results into a error message: "A dependency injection object is required to access the 'filter' service". Could you point me out, how to correctly us the "dependency injection object" :)

Thx!



2.0k

Ok, i figured it out myself. You need to pass the dependency injection object of the application to the Http\Request object. Nevertheless i thought, that the application will pass it automatically - is there a specific reason for it not to do it?

<?php

$di = new Phalcon\DI\FactoryDefault();
$app = new Phalcon\Mvc\Micro();

$app->setDI($di);

$app->get('/', function () use ($app) {
    $request = new Phalcon\Http\Request();
    $request->setDI($app->getDI());
    echo("->".$request->getQuery('id', 'int', 0));
});

$app->handle();

Disable line 10 and you will get the error message from above.



98.9k

A request object needs the dependency injector to obtain the 'filter' service, however, you don't need to instantiate that class, you can request it from the app DI:

<?php

$di = new Phalcon\DI\FactoryDefault();
$app = new Phalcon\Mvc\Micro();

$app->setDI($di);

$app->get('/', function () use ($app) {
    echo("->".$app['request']->getQuery('id', 'int', 0));
});

$app->handle();


2.0k
edited Oct '14

Thanks alot for the hint - i am using a "Phalcon\Mvc\Controller" and inside the class i just need to use "$this->request" to get a fully initialized request object. Nevertheless is it intended, that the default value only gets returned, when the query parameter is not used in the request? (using your code above and the "$this->request" object in a controller)

Example 1
Request: https://localhost/test.php?id=aa
Returned value: ""
Example 2
Request: https://localhost/test.php?id=a11a
Returned value: "11"
Example 3
Request: https://localhost/test.php?id=
Returned value: ""
Example 4
Request: https://localhost/test.php
Returned value: "0"

As in example 4 I would expect to get the default value in the examples 1 and 3 as well, since the given values have been filtered by the "int" filter



98.9k

Yes, it only use the default value when the parameter is not passed in the request, if it is passed it use the filter to perform additional transformations to the data,



2.0k

You really should consider to change that behaviour. A default value is suppose to be returned as soon as there is nothing to return - this is regardless if there is no parameter given, a parameter is empty or the filter emptied the parameter. The result is always the same - i do not get any value back and i need my default value.

In my case i need to implement additional checks every time i use that funtion, because i can not rely on it to at least return my default value. That will result into additional unnecessary lines of code.

Still - great work and i hope you guys keep it up. I like to use phalcon. (edit: I opened up an issue: https://github.com/phalcon/cphalcon/issues/1226)



13.9k

is it really necesary user Micro?

I only want to perform a PHQL from an external Class.

How can I achieve it?

A request object needs the dependency injector to obtain the 'filter' service, however, you don't need to instantiate that class, you can request it from the app DI:

<?php

$di = new Phalcon\DI\FactoryDefault();
$app = new Phalcon\Mvc\Micro();

$app->setDI($di);

$app->get('/', function () use ($app) {
   echo("->".$app['request']->getQuery('id', 'int', 0));
});

$app->handle();