1

I am trying to validate a key, which I am passing as url argument using service. But service function is not calling.

It's giving "Access denied" error. Am I missing something?

My sample code is:

cunn.routing.yml

cunnotification.saverecord:
  path: '/cunnotification/saverecord/{key}'
  defaults:
    _controller: '\Drupal\cunnotification\Controller\SaveRecordToDBController::SaveRecord'
  options:
    no_cache: TRUE
  requirements:
    _access_cunn_saverecord: 'TRUE'

cunn.services.yml

services:
  cunn.saverecord:
    class: Drupal\cunnotification\Access\CUNAccessCheck
    tags:
      - { name: cunn_access_check, applies_to: _access_cunn_saverecord }

CUNAccessCheck.php

namespace Drupal\cunn\Access;

use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Access check for conn routes.
 */
class CUNAccessCheck implements AccessInterface {

  /**
   * Checks access.
   *
   * @param string $key
   *   The conn key.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access($key) {
   // Logic here
   return TRUE; // based on logic
  }
}
1

Don't change they tag name, just access_check:

services:
  cunn.saverecord:
    class: Drupal\cunnotification\Access\CUNAccessCheck
    tags:
      - { name: access_check, applies_to: _access_cunn_saverecord }

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.