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!



1.3k

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.



81.7k

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();


1.3k
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: http://localhost/test.php?id=aa
Returned value: ""
Example 2
Request: http://localhost/test.php?id=a11a
Returned value: "11"
Example 3
Request: http://localhost/test.php?id=
Returned value: ""
Example 4
Request: http://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



81.7k

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,



1.3k

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)