I start banging my head on the table, the wall or anything else... please help me.

Here is my my_module.routing.yml:

  path: '/api/v1/{coupon_code}/validate'
    _controller: '\Drupal\my_module\API\ValidateCouponController::validate'
    _permission: 'access content'
        type: coupon_code_converter

This is my services.yml

    class: Drupal\my_module\Routing\CouponCodeConverter
      - { name: paramconverter }

This is my converter class - it is taken from Drupal\language\LanguageConverter and I added some breakpoints which should make the script stop in my IDE:

namespace Drupal\my_module\Routing;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\Routing\Route;

 * Converts a coupon code to a fully loaded coupon
class CouponCodeConverter implements ParamConverterInterface {

   * {@inheritdoc}
  public function convert($value, $definition, $name, array $defaults) {
    if (!empty($value)) {
      return $this->languageManager->getLanguage($value);
    return NULL;

   * {@inheritdoc}
  public function applies($definition, $name, Route $route) {
    return (!empty($definition['type']) && $definition['type'] == 'language');

And this is my controller class:

namespace Drupal\my_module\API;

use Drupal\Core\Controller\ControllerBase;

class ValidateCouponController extends ControllerBase  {

  public function validate($coupon_code) {
    $container = \Drupal::getContainer();
    $kernel = $container->get('kernel');
    $services = $kernel->getCachedContainerDefinition()['services'];
    return array(
      '#markup' => 'hallo'

I can enter http://mypath.com/api/v1/1/validate and the controller gets the value '1' for $coupon_code. But I want my CouponCodeConverter class to do some magic before the parameter gets sent to the Controller... but the converter gets not called... what do I wrong?


I think the condition in applies() doesn't match with the route parameters, try this:

  public function applies($definition, $name, Route $route) {
    return (!empty($definition['type']) && $definition['type'] == 'coupon_code_converter');
  • Thanks a lot... I placed a Breakpoint in the applies() method but the script does not stop there. Thank you – Tobias Krause May 31 '18 at 9:17
  • When do you expect this to happen? This is part of the route building process and usually doesn't run in a normal request. – 4k4 May 31 '18 at 9:35
  • Ah ok.. I though like the AccessController on every request this applies() function would be called. Thank you for that clarification. – Tobias Krause Jun 4 '18 at 6:32

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.