# Drupal: Check if the current page is using the admin theme

## Snippet
```php
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
  // ...
}
```

The [`router.admin_context`](https://api.drupal.org/api/drupal/core%21core.services.yml/service/router.admin_context/9.4.x) service maps to the [`AdminContext`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Routing%21AdminContext.php/class/AdminContext/9.4.x) helper class.

[`isAdminRoute()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Routing%21AdminContext.php/function/AdminContext%3A%3AisAdminRoute/9.4.x) simply checks the [`Route`](https://api.drupal.org/api/drupal/vendor%21symfony%21routing%21Route.php/class/Route/9.4.x) for the *_admin_route* option.

### Pitfalls

When working with a Route object, it's tempting to check for the `_admin_route` option directly, instead of using the `router_admin_context` service. The problem here is any [customization of the service](https://www.drupal.org/docs/drupal-apis/services-and-dependency-injection/altering-existing-services-providing-dynamic) will be ignored.

## Relevant links
- [Admin paths are now defined as part of route definitions](https://www.drupal.org/node/2224207): Drupal 7 to 8 change record, with a *Matching admin routes* section referring to this.
- [Structure of routes](https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-routes): Information about the *_admin_route* option (and Drupal routes in general).
- [How to check if the current page using admin theme in Drupal?](http://lobsterr.me/post/how-check-if-current-page-using-admin-theme-drupal): Similar blog post.
