Routing path with Entity Reference field I'm working on a custom module (my_module) which provides some functionality to a specific paragraph type (my_paragraph) created from the  user interface. In my routing file, I have the following route definition.

my_module_controller_action:
  path: '/node/{node}/my_paragraph/{paragraph}'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyModuleController::action'
    _title: 'page title'
  requirements:
    _permission: 'access content'


Now I add a new entity reference field (field_paragraph) to the Basic page content type. Using this field I can add my_paragraph along with other paragraph types. 

With this set up, I create a basic page, add a piece of my_paragraph, and saved it (/node/123). I can see my custom controller is not called (because now the path is /node/123), but if I go to /node/123/my_paragraph/1, my custom controller is getting called there and I get the output I want.


What should I do so that my custom controller can be used on /node/123 page?
If I add /about as path alias of /node/123, how do I achieve the same outcome? 


I'm actually refactoring an old custom module I wrote and trying to move away from the hooks approach if possible. One of the main motivations for me by refactoring is I can use one or more services (together with dependency injections) to separate all the possible logics here.
The problem here is that controllers are specifically intended to return a result based on a given path. The result you want isn't dependent on a path, but rather on an operation performed on an entity.

What you'll want to use instead is one of the entity API hooks, found here: Entity CRUD, editing and view hooks

To respond to your entity being created, I recommend using:
hook_ENTITY_TYPE_insert()

function my_module_node_insert($node) {
  // Get ids for all Paragraph references stored in your field.
  $paragraph_ids = $node-&gt;get('field_paragraph')-&gt;getValue();
  // Get Paragraph storage handler.
  $paragraph_storage = \Drupal::entityTypeManager()-&gt;getStorage('paragraph');
  // Load Paragraphs from their IDs.
  $paragraphs = $paragraph_storage-&gt;loadMultiple( array_column($paragraph_ids, 'target_id') );

  // Iterate through your loaded Paragraph instances...
  foreach($paragraphs as $paragraph) {
    // Do your stuff to the paragraph.
  }
}


Similarly, if you need to update field_paragraph when you update your node, implement hook_ENTITY_TYPE_update().