You are here

public function MenuExampleController::urlArgument in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/menu_example/src/Controller/MenuExampleController.php \Drupal\menu_example\Controller\MenuExampleController::urlArgument()

Demonstrates use of optional URL arguments in for menu item.

Parameters

string $arg1: First argument of URL.

string $arg2: Second argument of URL.

Return value

array Controller response.

See also

https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes

1 string reference to 'MenuExampleController::urlArgument'
menu_example.routing.yml in menu_example/menu_example.routing.yml
menu_example/menu_example.routing.yml

File

menu_example/src/Controller/MenuExampleController.php, line 173

Class

MenuExampleController
Controller routines for menu example routes.

Namespace

Drupal\menu_example\Controller

Code

public function urlArgument($arg1, $arg2) {

  // Perpare URL for single arguments.
  $url_single = Url::fromRoute('examples.menu_example.use_url_arguments', [
    'arg1' => 'one',
  ]);

  // Prepare URL for multiple arguments.
  $url_double = Url::fromRoute('examples.menu_example.use_url_arguments', [
    'arg1' => 'one',
    'arg2' => 'two',
  ]);

  // Add these argument links to the page content.
  $markup = $this
    ->t('This page demonstrates using arguments in the url. For example, access it with @link_single for single argument or @link_double for two arguments in URL', [
    '@link_single' => Link::createFromRoute($url_single
      ->getInternalPath(), $url_single
      ->getRouteName(), $url_single
      ->getRouteParameters())
      ->toString(),
    '@link_double' => Link::createFromRoute($url_double
      ->getInternalPath(), $url_double
      ->getRouteName(), $url_double
      ->getRouteParameters())
      ->toString(),
  ]);

  // Process the arguments if they're provided.
  if (!empty($arg1)) {
    $markup .= '<div>' . $this
      ->t('Argument 1 = @arg', [
      '@arg' => $arg1,
    ]) . '</div>';
  }
  if (!empty($arg2)) {
    $markup .= '<div>' . $this
      ->t('Argument 2 = @arg', [
      '@arg' => $arg2,
    ]) . '</div>';
  }

  // Finally return the markup.
  return [
    '#markup' => $markup,
  ];
}