Restrict Drupal route to be accessible only to authenticated user
There are multiple ways to deny non-authorized users - access to a route.
First think which come to mind would be to achieve this with a permission which would be granted to the authenticated role. This involves dealing with additional config but if we're looking for a pure YAML definition solution you have a few choices.
By defining the _user_is_logged_in
requirements
my.page:
path: '/my-path'
defaults:
_controller: '\Drupal\my\Controller\MyController::content'
_title: 'My Page'
requirements:
_user_is_logged_in: 'TRUE'
By requiring authenticated _role
in requirement
my.page:
path: '/my-path'
defaults:
_controller: '\Drupal\my\Controller\MyController::content'
_title: 'My Page'
requirements:
_role: 'authenticated'
Last, if you need extended logic in determining weather a User should be allowed accessing a route you should look into creating a custom Access check.