Skip to main content
Category:

in this tuto, I'll show you how to create a custom page at /custom-page-with-arguments/% that displays content based on arguments.

Example

mymodule.routing.yml

mymodule.custom-page-with-arguments:
  path: '/custom-page-with-arguments/{name}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\HelloController::index'
    _title: 'custom page with arguments'
    name: 'Codimth'
  requirements:
    _permission: 'access content'

default value of name is 'Codimth'.

src/Controller/HelloController.php

<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;

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

    /**
     * @param $name
     * @return array
     */
    public function index($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