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)),
    ];
  }

}

Comments

Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

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

Web Posts

Search

Page Facebook