Search code examples
phpdrupalhookdrupal-8drupal-hooks

Error when a new module created in Drupal 8


I created a custom module in Drupal 8 and my error logs gave me the following message:

ReflectionException: Class Drupal\onesignal_api\src\Controller\OneSignalApiController does not exist in ReflectionMethod->__construct() (line 123 of core/lib/Drupal/Core/Entity/EntityResolverManager.php)

here are my files:

onesignal_api.routing.yml:

onesignal_api:
 path: '/onesignal_api'
 defaults:
   _controller:'Drupal\onesignal_api\src\Controller\OneSignalApiController::hook_onesignal_api_insert'
_title: 'One Signal API'
requirements:
  _permission: ‘access content’ 

OneSignalApiController.php:

<?php

namespace Drupal\onesignal_api\Controller; 

class OneSignalApiController {

/***
* Hook into OneSignal API to send push notifications once a new node is created
*/


   function hook_onesignal_api_insert(Drupal\Core\Node\NodeAPI $node) {
      if($node->isNew()) {
        // Code to execute
      }
   }
}

What is the issue?


Solution

  • The problem is the value of _controller:

    'Drupal\onesignal_api\src\Controller\OneSignalApiController::hook_onesignal_api_insert'

    You need to remove the \src.

    That value should refer to the namespace, namespace Drupal\onesignal_api\Controller; not the path.


    Also, maybe something just got lost in translation while posting the question here, but the indentation is off in your routing file and you have some curly quotes around access content that need to be replaced with straight quotes.

    onesignal_api:
      path: '/onesignal_api'
      defaults:
        _controller: 'Drupal\onesignal_api\Controller\OneSignalApiController::hook_onesignal_api_insert'
        _title: 'One Signal API'
      requirements:
        _permission: 'access content'