Skip to main content

Check if user has access to a route

Posted on January 29, 2020


Use the access_manager service to check if the user has access to a route.

$access_manager = \Drupal::service('access_manager'); 
$has_access = $access_manager->checkNamedRoute($route_name, $route_parameters, $account);

For example, to checks if the current user has access to the canonical route of the node with id 8:

$access_manager = \Drupal::service('access_manager'); 
$has_access = $access_manager->checkNamedRoute('entity.node.canonical', ['node' => 8], \Drupal::currentUser());