Drupal 8 Routematch
- Note: Drupal::routeMatch-getRouteName will sometimes return null. If you are inside a class, to do things properly, you'll want to inject the route match service in the constructor and then call it this way.
- 9.1.x core/lib/Drupal/Core/Routing/RouteMatch.php DrupalCoreRoutingRouteMatch::getRouteName; 9.0.x core/lib/Drupal/Core/Routing/RouteMatch.php DrupalCore.
Drupal::routeMatch-getParameter is to get a route parameter outside of the controller, for example in a preprocess hook. For this to work the parameter has to be defined in the current route as well. Improve this answer. Follow edited Feb 27 '17 at 10:43. @MPD - it does, but you're most likely to get bitten by that issue if you're generating a URL or a link. Remember that Drupal uses stream wrappers a lot, especially since public files are accessed via the public:// stream wrapper type. Creating links to public files certainly blew up will into the 8.0rc series. For all I know, it still does.
To pass custom variables to twig file in drupal 8, you must create a preprocess function, for the template:
to print variable in twig file just use {{ my_variable }}.
Example how to pass variables to node twig file:
Example how to pass variables to page twig file:
Example how to pass variables to region twig file:

Example how to pass variables to menu twig file:
Drupal 8 Routematchinterface
Example how to pass variables to links twig file:
Example how to pass variables to views view unformatted twig file (views-view-unformatted.html.twig):
Example how to pass variables to block twig file (block.html.twig):
Example how to pass current language variable to select twig file (select.html.twig):
Example how to pass variable to menu twig file (menu.html.twig):
function rma_preprocess_block(&$variables) {
global $base_url;
Drupal 8 Create Routematch
$variables['base_path'] = $base_url;
Drupal 8 Routematch Getparameter
}