Skip to main content
Category:

Drupal 8's routes may include placeholder elements which designate places where the URL contains dynamic values. In the controller method, this value is available when a variable with the same name is used in the controller method.

For example in mymodule.routing.yml:

mymodule.route_name:
  path: '/mymodule/{name}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController::content'
  requirements:
    _permission: 'access content'

The {name} element in the URL is called a slug and is available as a $name in the content method.

Example:

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class MyModuleController
 * @package Drupal\mymodule\Controller
 */
class MyModuleController extends ControllerBase
{

  /**
   * @param $name
   * @return array
   */
  public function content($name)
  {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello @name .', array('@name' => $name)),
    ];
  }

}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook