function RoutePathGenerationTrait::generateRoutePath

Generates a route path by replacing placeholders with their values.

Placeholders without corresponding values in the parameters array are removed from the resulting path.

Parameters

\Symfony\Component\Routing\Route $route: The route object containing the path with placeholders.

array $parameters: An associative array of parameters to replace in the route path, where the keys are placeholders and the values are the replacement values.

Example:
[
    'parameter1' => 'value1',
];

This will transform a route path such as '/route/path/{parameter1}{parameter2}' into '/route/path/value1'.

Return value

string The generated path with all placeholders either replaced by their corresponding values or removed if no matching parameter exists.

2 calls to RoutePathGenerationTrait::generateRoutePath()
CsrfAccessCheck::access in core/lib/Drupal/Core/Access/CsrfAccessCheck.php
Checks access based on a CSRF token for the request.
RouteProcessorCsrf::processOutbound in core/lib/Drupal/Core/Access/RouteProcessorCsrf.php
Processes the outbound route.

File

core/lib/Drupal/Core/Access/RoutePathGenerationTrait.php, line 37

Class

RoutePathGenerationTrait
Provides a method for generating route paths.

Namespace

Drupal\Core\Access

Code

public function generateRoutePath(Route $route, array $parameters) : string {
    $path = ltrim($route->getPath(), '/');
    // Replace path parameters with their corresponding values from the
    // parameters array.
    foreach ($parameters as $param => $value) {
        if (NULL !== $value) {
            $path = str_replace("{{$param}}", $value, $path);
        }
    }
    // Remove placeholders that were not replaced.
    $path = preg_replace('/\\/{[^}]+}/', '', $path);
    // Remove trailing slashes (multiple slashes may result from the removal of
    // unreplaced placeholders).
    $path = rtrim($path, '/');
    return $path;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.