Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(How) Can filters / parameters be used with a DB-connected service in Apigility?

As the Apigility documentation (REST Service Tutorial -> Create a REST Service -> [infobox] Code-Connected vs DB-Connected services) says, the built-in functionality for DB-connected services is "more of a rapid application development (RAD) or prototyping tool."

Well, now I'm developing a very small application and think, this prototyping functionality might be enough. But even this small app needs a simple filtering / request parametrization, e.g.: items by name = 'foo', items by id < x and id > y, such stuff.

Do the DB-connected services provide filtering? If yes, how to handle this?

like image 446
automatix Avatar asked Dec 11 '25 21:12

automatix


1 Answers

For this you are going to use the zf-content-validation package (see https://apigility.org/documentation/modules/zf-content-validation).

With the content-validation module you are able to define input filters in your module configuration section and specify which input filter to use to validate your service requests.

For example (from the documentation):

'zf-content-validation' => array(
    'Application\Controller\HelloWorld' => array(
        'input_filter' => 'Application\Controller\HelloWorld\Validator',
        'POST' => 'Application\Controller\HelloWorld\CreationValidator',
    ),
),


'input_filter_specs' => array(
    'Application\Controller\HelloWorldGet' => array(
        0 => array(
            'name' => 'name',
            'required' => true,
            'filters' => array(
                0 => array(
                    'name' => 'Zend\Filter\StringTrim',
                    'options' => array(),
                ),
            ),
            'validators' => array(),
            'description' => 'Hello to name',
            'allow_empty' => false,
            'continue_if_empty' => false,
        ),
    ),
),

This configuration will validate that requests to your HelloWorld controller will include a "name" parameter that is a string and it will additionally trim the whitespace for you. If your request had the query parameter ?name=foo your controller will get foo for the name parameter. Or, if your request doesn't include a name parameter, you'll get an ApiProblem response indicating that the request didn't pass validation.

like image 157
Jeremy Giberson Avatar answered Dec 14 '25 12:12

Jeremy Giberson