n Using parameters in routes in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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