1

I'm working on a controller that queries a table based on the value in a route parameter

Example route path:

path: '/path/{parameter}'

Controller:

  public function myFunction($parameter) {
    $connection = \Drupal::database();
    $query = $connection->select('my_custom_table', 'mct');
    $query->condition('mct.col', $parameter, '=');
    ...
    $result = $query->execute();
    $records = $result->fetchAll();

Is there any sanitization that should occur in this circumstance or does the database API take care of this?

3

For input, saving to the DB, Database API takes care of it, automatically prevents SQL injection when using dynamic query. Since your code is using dynamic query, you're all good.

If you were to use static query you have to make sure you use placeholders as the docs mention.

For output, to prevent XSS, you'll want to use a template file, and print your value via Twig. Which auto escapes everything by default. If you don't use Twig, then you need to use the appropriate API function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.