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

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon