Get node from url | Drupal 8

Get node from url

Submitted by editor on Wed, 12/02/2015 - 14:02
Question

How to get the node or node ID (nid) from url ?

Example:

$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
  $nid = $node->id();
}

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.